-
-
Notifications
You must be signed in to change notification settings - Fork 725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow sessions to work across subdomains #8090
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
# Be sure to restart your server when you modify this file. | ||
|
||
# The cookie_store can be too small for very long URLs stored by Devise. | ||
# The maximum size of cookies is 4096 bytes. | ||
#Openfoodnetwork::Application.config.session_store :cookie_store, key: '_openfoodnetwork_session' | ||
|
||
# Use the database for sessions instead of the cookie-based default, | ||
# which shouldn't be used to store highly confidential information | ||
# (create the session table with "rails generate session_migration") | ||
Openfoodnetwork::Application.config.session_store :active_record_store | ||
Openfoodnetwork::Application.config.session_store( | ||
:active_record_store, | ||
key: "_ofn_session_id", | ||
domain: :all, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the line that does the magic, correct? By passing these additional arguments it shares cookies across all subdomains? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, the The name change is necessary here, as you can't modify the primary attributes of an existing cookie with the same name. If we didn't do it the changes wouldn't take effect until the user fully logged out and back in, and we don't know when that would happen. |
||
tld_length: 2 | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
class SessionCookieUpgrader | ||
def initialize(app, options = {}) | ||
@app = app | ||
@options = options | ||
end | ||
|
||
def call(env) | ||
request = ::Rack::Request.new(env) | ||
cookies = request.cookies | ||
old_key = @options[:old_key] | ||
new_key = @options[:new_key] | ||
|
||
# Set the session id for this request from the old session cookie (if present) | ||
# This must be done before @app.call(env) or a new session will be initialized | ||
cookies[new_key] = cookies[old_key] if cookies[old_key] | ||
|
||
status, headers, body = @app.call(env) | ||
|
||
if cookies[old_key] | ||
# Create new session cookie with pre-existing session id | ||
Rack::Utils.set_cookie_header!( | ||
headers, | ||
new_key, | ||
{ value: cookies[old_key], path: "/", domain: @options[:domain] } | ||
) | ||
|
||
# Delete old session cookie | ||
Rack::Utils.delete_cookie_header!(headers, old_key) | ||
end | ||
|
||
[status, headers, body] | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Commented code 👃 🔥