class ApplicationController < ActionController::Base AUTH_TOKEN_COOKIE = :auth_token # Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has. allow_browser versions: :modern # Changes to the importmap will invalidate the etag for HTML responses stale_when_importmap_changes before_action :rotate_access_key_if_needed before_action :load_current_user before_action :require_identified_user helper_method :current_user, :identified? private def rotate_access_key_if_needed AccessKey.rotate_if_needed! end def load_current_user Current.user_session = UserSession.find_by_token(cookies.encrypted[AUTH_TOKEN_COOKIE]) Current.user = Current.user_session&.user Current.user_session&.touch(:last_used_at) end def require_identified_user return if identified? redirect_to identify_path, alert: "Identify yourself before using Car Tracker." end def current_user Current.user end def identified? current_user.present? end def sign_in(user) user_session, raw_token = UserSession.create_for!(user) Current.user_session = user_session Current.user = user cookies.encrypted[AUTH_TOKEN_COOKIE] = { value: raw_token, httponly: true, same_site: :lax } end def sign_out Current.user_session&.destroy! Current.user_session = nil Current.user = nil cookies.delete(AUTH_TOKEN_COOKIE) end end