Files
car-tracker/app/controllers/application_controller.rb
bionickatana 0826d267f2
CI / scan_ruby (push) Failing after 1m5s
CI / scan_js (push) Failing after 6s
CI / lint (push) Failing after 1m29s
Finished phase005
2026-09-16 15:34:50 -06:00

60 lines
1.4 KiB
Ruby

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