2024-02-05 06:05:13 +11:00
|
|
|
module Authentication
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2024-04-03 10:35:55 -04:00
|
|
|
before_action :authenticate_user!
|
|
|
|
end
|
|
|
|
|
|
|
|
class_methods do
|
|
|
|
def skip_authentication(**options)
|
|
|
|
skip_before_action :authenticate_user!, **options
|
|
|
|
end
|
2024-02-05 06:05:13 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def authenticate_user!
|
2024-02-05 10:36:46 +11:00
|
|
|
if user = User.find_by(id: session[:user_id])
|
|
|
|
Current.user = user
|
|
|
|
else
|
|
|
|
redirect_to new_session_url
|
|
|
|
end
|
2024-02-05 06:05:13 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
def login(user)
|
|
|
|
Current.user = user
|
|
|
|
reset_session
|
|
|
|
session[:user_id] = user.id
|
2024-07-22 15:37:03 +02:00
|
|
|
set_last_login_at
|
2024-02-05 06:05:13 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
def logout
|
|
|
|
Current.user = nil
|
|
|
|
reset_session
|
|
|
|
end
|
2024-04-04 23:00:12 +02:00
|
|
|
|
|
|
|
def set_last_login_at
|
|
|
|
Current.user.update(last_login_at: DateTime.now)
|
|
|
|
end
|
2024-02-05 06:05:13 +11:00
|
|
|
end
|