Subdomains in a Rails 4 app

前端 未结 2 2064
旧巷少年郎
旧巷少年郎 2020-12-30 17:43

Today I came across a rather weird phenomenon. When developing a Rails app in which every user has his own subdomain, and trying to use Devise to do it, I encountered that a

2条回答
  •  萌比男神i
    2020-12-30 18:45

    There are several issues you may have

    The first is you need to appreciate the way in which Devise redirects your user after login, and secondly how subdomains are routed in Rails.

    --

    Devise

    By default, Devise routes to current_user_path (which typically means users#show) or something in your routes:

    def after_sign_in_path_for(resource)
      current_user_path
    end
    

    This means when you accept the login from the user, they will be taken to their own path. Depending on your routes, this will generally mean the main site (no subdomain) user's path (domain.com/users/56) or something.

    Without any specifics on this from your question, I can only speculate on this.

    --

    Subdomains

    Having just worked on some subdomain-enabled apps, there is something you should consider about routing to subdomains.

    Once your user has signed in, they need to be able to be routed to a specific subdomain. The way to do this is to use a constraint in your routes:

    #config/routes.rb
    constraints { subdomain: 'admin' } do
        resources :photos
    end
    

    We've found you cannot do this using normal routing paths - you have to use the url (not path helper). For example:

    photos_path(subdomain: current_user.name) #-> does not work (path is relative)
    photos_url(subdomain: current_user.name) #-> will route to http://name.lvh.me:3000
    

    What you have to remember is if you're looking to redirect / route traffic to different subdomains, you will need to reference the url form of the helper, not the path reference.

    So if you take the after_sign_in_path_for as shown above, you'll want to do something like this:

    def after_sign_in_path_for(resource)
       root_url(subdomain: resource.name)
    end
    

    --

    Sessions

    Finally, you want to ensure your Devise session cookies will remain initialized after you've set them. We've found subdomains aren't handled by default, so you have to ensure they are catered for:

    Share session (cookies) between subdomains in Rails?

    #config/initializers/session_store.rb
    YOUR_APP_NAME::Application.config.session_store :cookie_store, key: '_app_name_session', domain: :all, tld_length: 2 
    

提交回复
热议问题