Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to isolate cookie store for one subdomain from other subdomains and domain itself in Rails 5?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 15
    Answer it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: