Hello,
I need to have cookie store #1 on a.example.com
and I need to have cookie store #2 on b.example.com and on example.com
One method was to change cookie name per subdomain / domain, however
request.env['rack.session.options'][:key] = "custom_name"
does not seem to work with Rails 5.
Another method was to dynamically set cookie domain names with middleware
class DomainSession
def initialize(app)
@app = app
end
def call(env)
if env["SERVER_NAME"].start_with?("system.")
env["rack.session.options"][:domain] = ".#{env["SERVER_NAME"]}"
else
env["rack.session.options"][:domain] = env["SERVER_NAME"].split(".").size < 3 ?
".#{env["SERVER_NAME"]}" :
".#{env["SERVER_NAME"].split(".")[1..-1].join(".")}"
end
@app.call(env)
end
end
However, this does not work too, because ".example.com" includes "system" as for its "." in the beginning, I think that's how cookies work.
Could you help? Thanks.
0 Answer(s)