Skip to content
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

Adds current_active_session helper, memoization for current_user #95

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions app/controllers/active_sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ class ActiveSessionsController < ApplicationController
before_action :authenticate_user!

def destroy
@active_session = current_user.active_sessions.find(params[:id])
active_session = current_user.active_sessions.find(params[:id])

@active_session.destroy

if current_user
redirect_to account_path, notice: "Session deleted."
else
if active_session == current_active_session
forget_active_session
active_session.destroy

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we still call active_session.destroy before the if statement? I only ask because we're calling it in both branches.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That bothered me as it doesn't feel DRY, but there's no good way to handle it. "current_active_session" will certainly be memoized by the time "destroy" is called because of "authenticate_user!". But "current_active_session" has code to do a lookup for the comparison, and destroying it before then creates a dependency on "authenticate_user!" being called first.

So, yes, we can call it before the "if" statement, but I prefer everything to be explicit at the expense of an extra line. This might save a headache later during refactoring.

reset_session
redirect_to root_path, notice: "Signed out."
else
active_session.destroy
redirect_to account_path, notice: "Session deleted."
end
end

Expand Down
11 changes: 8 additions & 3 deletions app/controllers/concerns/authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Authentication

included do
before_action :current_user
helper_method :current_active_session
helper_method :current_user
helper_method :user_signed_in?
end
Expand Down Expand Up @@ -41,10 +42,14 @@ def remember(active_session)
private

def current_user
Current.user = if session[:current_active_session_id].present?
ActiveSession.find_by(id: session[:current_active_session_id])&.user
Current.user ||= current_active_session&.user
end

def current_active_session
Current.active_session ||= if session[:current_active_session_id].present?
ActiveSession.find_by(id: session[:current_active_session_id])
elsif cookies[:remember_token]
ActiveSession.find_by(remember_token: cookies.encrypted[:remember_token])&.user
ActiveSession.find_by(remember_token: cookies.encrypted[:remember_token])
end
end

Expand Down
1 change: 1 addition & 0 deletions app/models/current.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class Current < ActiveSupport::CurrentAttributes
attribute :user
attribute :active_session
end
Loading