Rails - Dynamic cookie domains using Rack
- by Tim B.
I'm fairly new to Rails and Rack, but this guy had a seemingly straightforward write-up about using Rack to implement dynamic session domain middleware. The code looks good to and I've implemented it here on my local machine, but I'm still not able to transcend top level domains on a single login.
Here's the middleware code:
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
And then in environment.db:
config.load_paths += %W(#{RAILS_ROOT}/app/middlewares)
Lastly in production.db (and development.db):
config.middleware.use "SetCookieDomain", ".example.org"
Any help is greatly appreciated.
EDIT: I'm running Rails 2.3.3 and Rack 1.0