38 lines
857 B
Ruby
38 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
|