Skip to content

Commit a3a05a9

Browse files
Store session_id in cookies only once (rails#52831)
Before this commit, while authenticated, the `set_current_session` was called on every request which resulted in setting the `session_id` in the cookies on every request. ```rb def set_current_session(session) Current.session = session cookies.signed.permanent[:session_id] = { value: session.id, httponly: true, same_site: :lax } end ``` Setting the `session_id` in the cookies on every request is unnecessary. Also, if developers want to set an expiration date to the cookies (for example with `expires: 1.week`), the expiration date will be reset on every request leading to the cookies never expiring. This PR solves the issue by storing the `session_id` in the cookies only in the `start_new_session_for` method.
1 parent d955fd6 commit a3a05a9

File tree

1 file changed

+1
-1
lines changed
  • railties/lib/rails/generators/rails/authentication/templates/controllers/concerns

1 file changed

+1
-1
lines changed

railties/lib/rails/generators/rails/authentication/templates/controllers/concerns/authentication.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ def after_authentication_url
4646
def start_new_session_for(user)
4747
user.sessions.create!(user_agent: request.user_agent, ip_address: request.remote_ip).tap do |session|
4848
set_current_session session
49+
cookies.signed.permanent[:session_id] = { value: session.id, httponly: true, same_site: :lax }
4950
end
5051
end
5152

5253
def set_current_session(session)
5354
Current.session = session
54-
cookies.signed.permanent[:session_id] = { value: session.id, httponly: true, same_site: :lax }
5555
end
5656

5757
def terminate_session

0 commit comments

Comments
 (0)