Skip to content

Commit 417c014

Browse files
committedFeb 4, 2022
Use safe navigation operator to conditionally check if a user exists
This is needed because we're now allowing a user to delete any of their active sessions. This means that is a user is logged into multiple devices, their session could be deleted while they're still using the application mid-request.
1 parent 9e65b86 commit 417c014

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed
 

‎app/controllers/concerns/authentication.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def store_location
4646

4747
def current_user
4848
Current.user = if session[:current_active_session_id].present?
49-
ActiveSession.find_by(id: session[:current_active_session_id]).user
49+
ActiveSession.find_by(id: session[:current_active_session_id])&.user
5050
elsif cookies.permanent.encrypted[:remember_token].present?
5151
User.find_by(remember_token: cookies.permanent.encrypted[:remember_token])
5252
end

‎test/test_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class ActiveSupport::TestCase
1111

1212
# Add more helper methods to be used by all tests here...
1313
def current_user
14-
session[:current_active_session_id] && ActiveSession.find_by(id: session[:current_active_session_id]).user
14+
session[:current_active_session_id] && ActiveSession.find_by(id: session[:current_active_session_id])&.user
1515
end
1616

1717
def login(user, remember_user: nil)

0 commit comments

Comments
 (0)
Failed to load comments.