diff options
author | Matthew Lemon <y@yulqen.org> | 2024-11-11 16:19:44 +0000 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-11-11 16:19:44 +0000 |
commit | 97201e0bca203b6b303789374ffd7e4228e062a7 (patch) | |
tree | 1d70b0c499c514ceccd906b6b6e85dcbacbd3f45 /app/controllers/concerns/authentication.rb |
initial after migration
Diffstat (limited to 'app/controllers/concerns/authentication.rb')
-rw-r--r-- | app/controllers/concerns/authentication.rb | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/app/controllers/concerns/authentication.rb b/app/controllers/concerns/authentication.rb new file mode 100644 index 0000000..771b21d --- /dev/null +++ b/app/controllers/concerns/authentication.rb @@ -0,0 +1,55 @@ +module Authentication + extend ActiveSupport::Concern + + included do + before_action :require_authentication + helper_method :authenticated? + end + + class_methods do + def allow_unauthenticated_access(**options) + skip_before_action :require_authentication, **options + end + end + + private + def authenticated? + resume_session + end + + def require_authentication + resume_session || request_authentication + end + + + def resume_session + Current.session ||= find_session_by_cookie + end + + def find_session_by_cookie + Session.find_by(id: cookies.signed[:session_id]) + end + + + def request_authentication + session[:return_to_after_authenticating] = request.url + redirect_to new_session_path + end + + def after_authentication_url + session.delete(:return_to_after_authenticating) || root_url + end + + + def start_new_session_for(user) + user.sessions.create!(user_agent: request.user_agent, ip_address: request.remote_ip).tap do |session| + Current.session = session + cookies.signed.permanent[:session_id] = { value: session.id, httponly: true, same_site: :lax } + end + end + + def terminate_session + Current.session.destroy + cookies.delete(:session_id) + end +end |