Dynamic session domain using Rack

Rails: 2.3.3

Overview: I want to enable cross domain session support. Eventually,
I’ll have several domains for users to choose from, i.e.
user.domain1.com or user.domain2.com. I’ve borrowed from the code below,
but it doesn’t seem to be working.

In middleware:

class SetCookieDomain

def initialize(app, default_domain)
@app = app
@default_domain = default_domain
end

def call(env)
host = env[“HTTP_HOST”].split(’:’).first
env[“rack.session.options”][:domain] = custom_domain?(host) ?
“.#{host}” : “#{@default_domain}”
@app.call(env)
end

def custom_domain?(host)
domain = @default_domain.sub(/^./, ‘’)
host !~ Regexp.new("#{domain}$", Regexp::IGNORECASE)
end
end

In environment.rb:

config.load_paths += %W(#{RAILS_ROOT}/app/middlewares)

In development.rb:

config.middleware.use “SetCookieDomain”, “.mydomain.com”

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs