Rails - Dynamic cookie domains using Rack
Posted
by Tim B.
on Stack Overflow
See other posts from Stack Overflow
or by Tim B.
Published on 2009-07-30T03:56:57Z
Indexed on
2010/03/12
4:27 UTC
Read the original article
Hit count: 182
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
© Stack Overflow or respective owner