Search code examples
ruby-on-railsruby-on-rails-3cookiessession-cookiesactiondispatch

Configuring Rails App to handle multiple subdomains and multiple cookies


I have a rails app which supports multiple domains and each domain may have multiple subdomains.

Users visiting mydomain1.com do not receive the same experience as mydomain2.com (although the base behaviour of the apps is the same)

Therefore, if a user is logged in to mydomain1.com, it shouldn't then be logged in to mydomain2.com

If a user is logged in to france.mydomain1.com, it should then be logged in to germany.mydomain1.com

Previously, I've handled this by setting the domain in the session store configs:

MyApp::Application.config.session_store :cookie_store, :key => '_MyApp_session', :domain => APP_CONFIG[:domain]

I'm trying to work out the best way to handle this with multiple domains?

I've tried hacking around ActionDispatch::Callback but the request is not available from within there.

Can anybody suggest a good way of supporting multiple cookies from within one app?

Ideally I'd like to create a fresh cookie for each subdomain.


Solution

  • You should do that:

    class ActionDispatch::Session::MultiDomainStore < ActionDispatch::Session::CookieStore
      def initialize(app, options = {})       
        super(app, options.merge!(:domain => compute_domain(app)))      
      end
    
      def compute_domain(app)
        ...
      end
    end
    
    MyApp::Application.config.session_store :multi_domain_store, :key => '_MyApp_session'
    

    I.e. your domain should start with the dot.