Rails: how to retain session when redirecting to canonical domain (e.g. company.example.com -> example.com) -


rails 3.2.12, ruby 1.9.3

we allow users specify company using subdomain, mycompany.example.com, redirect canonical example.com , need remember user mycompany.

we have our environment set config.session_store contains :domain => 'example.com (an alternative works :domain => :all, :tld_length => 2) , supposed work allow sharing of session information between subdomains. there number of great posts, such one: share session (cookies) between subdomains in rails?

but before redirect sending session.inspect log, , it's getting different session (two separate session ids, etc.). basic issue cannot use session remember mycompany part before strip off.

i can work around that, there number of cases same user multiple companies (and part of our support team needs able switch companies).

i have tried on chrome , safari on os x. using "pow" local development environment has domain example.dev helps rule out several issues (vs. normal localhost:3000 server).

am missing something? indeed possible share cookie across domains?

update:

example code called in before_filter defined in applicationcontroller:

def redirect_to_canonical_if_needed   logger.debug "starting before_filter. session contains: #{session}"   if request.host != 'example.com'     session[:original_domain] = "originally came #{request.host}"     logger.debug "redirecting, session contains: #{session}"     redirect_to 'http://example.com', :status => :moved_permanently   end end 

setting added config/environments/production.rb , removed config/initializers/session_store.rb

  config.session_store = { :key => 'example_session', :secret => "secret", :domain => :all, :tld_length => 2 } 

or

  config.session_store = { :key => 'example_session', :secret => "secret", :domain => 'example.com' } 

and logging result, if start fresh environment no session exists going url a.example.com:

starting before_filter, session contains: {} redirecting, session contains: {"session_id"=>"4de9b56fb540f7295cd3192cef07ba63", "original_domain"=>"a.example.com"} filter chain halted :redirect_to_canonical_if_needed rendered or redirected completed 301 moved permanently in 2294ms (activerecord: 855.7ms) started "/" 123.456.789.123 @ 2013-07-12 09:41:12 -0400 processing homecontroller#index html   parameters: {} starting before_filter, session contains: {} 

so before filter fires on each new request. first request there's no session, hence "not loaded" message. test need redirect true. put in session , gets id , put in it. redirect. new request occurs on root domain, before filter fires again, , here's issue: session not initialized

this should work fine between 2 have setup following on dev

application @ example.dev

i view , set session variable @ a.example.dev visit b.example.dev , set long when (as describe) set domain 'example.dev' session store

this code in root controller/action describing

unless request.subdomain.to_s == 'another'   session[:original_domain] = request.subdomain.to_s   redirect_to 'http://another.' + request.domain.to_s   end 

and viewing original_domain available in session

if put example code in can have pitfalls


Comments