Performance: Session-based authentication
This affects page load speed and interaction responsiveness by managing user state on the server and sending session cookies with requests.
class ApplicationController < ActionController::Base before_action :load_user def load_user @current_user ||= User.find_by(id: session[:user_id]) end end
class ApplicationController < ActionController::Base before_action :load_user def load_user @current_user = User.find(session[:user_id]) if session[:user_id] end end
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Loading user on every request | 0 (server-side) | 0 | 0 | [X] Bad |
| Memoized user loading | 0 (server-side) | 0 | 0 | [OK] Good |
| Large, insecure cookies | 0 | 0 | 0 | [X] Bad |
| Optimized secure cookies | 0 | 0 | 0 | [OK] Good |
session[:user_id] typically store in a Rails application using session-based authentication?session[:user_id] holdsdef create
user = User.find_by(email: params[:email])
if user&.authenticate(params[:password])
session[:user_id] = user.id
redirect_to dashboard_path
else
render :new
end
endauthenticate.def logout session[:user] = nil redirect_to root_path end
session[:user_id] to track logged-in user.session[:user] to nil, which does not clear the actual user ID stored in session[:user_id].session[:user_id] exists before allowing access.session[:user_id] is missing, redirect to login page to block access.Pass the test and this pattern moves unknown