What does Rails 3 session_store domain :all really do?

后端 未结 5 774
抹茶落季
抹茶落季 2020-11-28 19:09

Updated question to make it more clear

I understand that you can set the domain of your session_store to share sessions between subdomains like this

5条回答
  •  情深已故
    2020-11-28 20:08

    OK, the way to accomplish this is to set the domain on the session cookie dynamically. To do this early enough it should be done as rack middleware:

    # Custom Domain Cookie
    #
    # Set the cookie domain to the custom domain if it's present
    class CustomDomainCookie
      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)
        host !~ /#{@default_domain.sub(/^\./, '')}/i
      end
    end
    

提交回复
热议问题