Last active
March 6, 2021 14:34
-
-
Save yshmarov/85a9a3e4572cac1b5cf682d42fb5449d to your computer and use it in GitHub Desktop.
subdomains
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
constraints SubdomainRequired do | |
root to: 'dashboards#show', as: 'subdomain_root' | |
end | |
class SubdomainRequired | |
def self.matches?(request) | |
request.subdomain.present? && request.subdomain != "www" | |
end | |
end | |
ApplicationController | |
before_action :redirect_to_subdomain # 1. | |
private | |
def redirect_to_subdomain | |
return unless user_signed_in? # 2. | |
if request.subdomain.blank? # 3. | |
if current_user.accounts.any? | |
redirect_to root_url(subdomain: current_user.accounts.first.subdomain) | |
end | |
else # 4. | |
unless current_user.accounts.pluck(:subdomain).include?(request.subdomain) | |
redirect_to root_url(subdomain: current_user.accounts.first.subdomain) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment