budget/app/controllers/concerns/authorizable.rb
Andrew Tomaka 108959f2b0
Some checks failed
Ruby CI / test (pull_request) Failing after 40s
Require authentication for most actions
2024-09-08 20:53:07 -04:00

37 lines
857 B
Ruby

module Authorizable
extend ActiveSupport::Concern
included do
before_action :require_registered_user
end
class_methods do
def allow_unregistered_user(**args)
skip_before_action :require_registered_user, **args
end
def require_unregistered_user(**args)
skip_before_action :require_registered_user, **args
before_action :require_unregistered_user, **args
end
end
private
def require_registered_user
Current.user.registered? || redirect_to_sign_in
end
def require_unregistered_user
Current.user.unregistered? || redirect_to_dashboard
end
def redirect_to_sign_in
session[:return_url] = request.url
redirect_to new_session_url, alert: "You must be logged in to continue."
end
def redirect_to_dashboard
redirect_to root_url, alert: "You are already logged in."
end
end