From 23eaa3e06661483f3fe6bb3b6a0d3d5e8a4d731d Mon Sep 17 00:00:00 2001 From: Dwight Watson Date: Mon, 5 Feb 2024 06:05:13 +1100 Subject: [PATCH] Split authentication concern from ApplicationController (#289) * Split authentication concern from ApplicationController * Remove empty space * Rubocop fixes --- app/controllers/application_controller.rb | 36 ++----------------- app/controllers/concerns/authentication.rb | 42 ++++++++++++++++++++++ 2 files changed, 44 insertions(+), 34 deletions(-) create mode 100644 app/controllers/concerns/authentication.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 4f7214fe..4d0fae2e 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,43 +1,11 @@ class ApplicationController < ActionController::Base + include Authentication + # Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has. allow_browser versions: :modern private - def authenticate_user! - redirect_to new_session_path unless user_signed_in? - end - - def current_user - Current.user || authenticate_user_from_session - end - helper_method :current_user - - def current_family - current_user.family - end - helper_method :current_family - - def authenticate_user_from_session - User.find_by(id: session[:user_id]) - end - - def user_signed_in? - current_user.present? - end - helper_method :user_signed_in? - - def login(user) - Current.user = user - reset_session - session[:user_id] = user.id - end - - def logout - Current.user = nil - reset_session - end - def hosted_app? ENV["HOSTED"] == "true" end diff --git a/app/controllers/concerns/authentication.rb b/app/controllers/concerns/authentication.rb new file mode 100644 index 00000000..e568a56f --- /dev/null +++ b/app/controllers/concerns/authentication.rb @@ -0,0 +1,42 @@ +module Authentication + extend ActiveSupport::Concern + + included do + helper_method :current_user + helper_method :current_family + helper_method :user_signed_in? + end + + private + + def authenticate_user! + redirect_to new_session_path unless user_signed_in? + end + + def current_user + Current.user || authenticate_user_from_session + end + + def current_family + current_user.family + end + + def authenticate_user_from_session + User.find_by(id: session[:user_id]) + end + + def user_signed_in? + current_user.present? + end + + def login(user) + Current.user = user + reset_session + session[:user_id] = user.id + end + + def logout + Current.user = nil + reset_session + end +end