Performance: Login and logout flow
This affects page load speed and interaction responsiveness during user authentication.
def create user = User.find_by(email: params[:email]) if user&.authenticate(params[:password]) session[:user_id] = user.id respond_to do |format| format.turbo_stream { render turbo_stream: turbo_stream.replace('login_form', partial: 'logged_in') } format.html { redirect_to root_path } end else flash.now[:alert] = 'Invalid credentials' render :new end end
def create user = User.find_by(email: params[:email]) if user&.authenticate(params[:password]) session[:user_id] = user.id redirect_to root_path else flash.now[:alert] = 'Invalid credentials' render :new end end
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Full page reload on login/logout | High (full DOM rebuild) | 1+ (full layout) | High (full repaint) | [X] Bad |
| Partial DOM update with Turbo Streams | Low (targeted DOM changes) | 0-1 (localized layout) | Low (partial repaint) | [OK] Good |
session[:user_id] typically store in a Rails login flow?session[:user_id] is used to store the unique identifier of the logged-in user to keep track of their session.reset_session method clears the entire session safely and is the recommended way to log out a user.session.delete(:user_id) or setting it to nil removes the user ID, reset_session is more secure as it resets the whole session.def create
user = User.find_by(email: params[:email])
if user&.authenticate(params[:password])
session[:user_id] = user.id
redirect_to dashboard_path
else
flash[:alert] = 'Invalid login'
render :new
end
enduser&.authenticate(params[:password]) returns false if password is wrong, so the else block runs.render :new.def destroy session[:user_id] = nil redirect_to login_path end
session[:user_id] = nil only removes the user ID but leaves other session data intact.reset_session to clear all session data and prevent session fixation attacks.session[:user_id] is missing (nil or false).before_action :require_login
def require_login
redirect_to login_path unless session[:user_id]
end redirects to login unless user_id exists, correctly blocking access. Others have incorrect conditions or redirect to wrong paths.Pass the test and this pattern moves unknown