Skip to content

Commit

Permalink
respond configuration method
Browse files Browse the repository at this point in the history
most/all success responses to POST actions involve setting a flash
message and a redirect response. This abstract it by having a method
which does both, and can be overridden if the user wants to do something
else, like rendering a success page, run some js, smth else.
  • Loading branch information
HoneyryderChuck committed Oct 7, 2023
1 parent 6243d91 commit 211e81a
Show file tree
Hide file tree
Showing 18 changed files with 96 additions and 45 deletions.
11 changes: 11 additions & 0 deletions lib/rodauth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,17 @@ def view(page, title, name=feature_name)
auth_methods meth
end

def response(name=feature_name)
meth = :"#{name}_response"
notice_flash_meth = :"#{name}_notice_flash"
redirect_meth = :"#{name}_redirect"
define_method(meth) do
set_notice_flash send(notice_flash_meth)
redirect send(redirect_meth)
end
auth_methods meth
end

def loaded_templates(v)
define_method(:loaded_templates) do
super().concat(v)
Expand Down
4 changes: 2 additions & 2 deletions lib/rodauth/features/change_login.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module Rodauth
additional_form_tags
button 'Change Login'
redirect
response

auth_value_methods :change_login_requires_password?

Expand Down Expand Up @@ -51,9 +52,8 @@ module Rodauth
end

after_change_login
set_notice_flash change_login_notice_flash
redirect change_login_redirect
end
change_login_response
end

set_error_flash change_login_error_flash
Expand Down
4 changes: 2 additions & 2 deletions lib/rodauth/features/change_password.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Rodauth
additional_form_tags
button 'Change Password'
redirect
response

translatable_method :new_password_label, 'New Password'
auth_value_method :new_password_param, 'new-password'
Expand Down Expand Up @@ -56,8 +57,7 @@ module Rodauth
set_password(password)
after_change_password
end
set_notice_flash change_password_notice_flash
redirect change_password_redirect
change_password_response
end

set_error_flash change_password_error_flash
Expand Down
4 changes: 2 additions & 2 deletions lib/rodauth/features/close_account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Rodauth
after
before
redirect
response

auth_value_method :account_closed_status_value, 3

Expand Down Expand Up @@ -50,8 +51,7 @@ module Rodauth
end
clear_session

set_notice_flash close_account_notice_flash
redirect close_account_redirect
close_account_response
end

set_error_flash close_account_error_flash
Expand Down
4 changes: 2 additions & 2 deletions lib/rodauth/features/confirm_password.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Rodauth
button 'Confirm Password'
before
after
response
redirect(:password_authentication_required){confirm_password_path}

session_key :confirm_password_redirect_session_key, :confirm_password_redirect
Expand All @@ -37,8 +38,7 @@ module Rodauth
confirm_password
after_confirm_password
end
set_notice_flash confirm_password_notice_flash
redirect confirm_password_redirect
confirm_password_response
else
set_response_error_reason_status(:invalid_password, invalid_password_error_status)
set_field_error(password_param, invalid_password_message)
Expand Down
4 changes: 2 additions & 2 deletions lib/rodauth/features/create_account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Rodauth
button 'Create Account'
additional_form_tags
redirect
response

auth_value_method :create_account_autologin?, true
translatable_method :create_account_link_text, "Create a New Account"
Expand Down Expand Up @@ -79,8 +80,7 @@ module Rodauth
if create_account_autologin?
autologin_session('create_account')
end
set_notice_flash create_account_notice_flash
redirect create_account_redirect
create_account_response
end
end

Expand Down
7 changes: 4 additions & 3 deletions lib/rodauth/features/lockout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ module Rodauth
notice_flash "Your account has been unlocked", 'unlock_account'
notice_flash "An email has been sent to you with a link to unlock your account", 'unlock_account_request'
redirect :unlock_account
response :unlock_account
redirect(:unlock_account_request){default_post_email_redirect}
redirect(:unlock_account_email_recently_sent){default_post_email_redirect}
email :unlock_account, 'Unlock Account'



auth_value_method :unlock_account_autologin?, true
auth_value_method :max_invalid_logins, 100
auth_value_method :account_login_failures_table, :account_login_failures
Expand Down Expand Up @@ -134,8 +136,7 @@ module Rodauth
end

remove_session_value(unlock_account_session_key)
set_notice_flash unlock_account_notice_flash
redirect unlock_account_redirect
unlock_account_response
else
set_response_error_reason_status(:invalid_password, invalid_password_error_status)
set_field_error(password_param, invalid_password_message)
Expand Down
10 changes: 9 additions & 1 deletion lib/rodauth/features/login.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module Rodauth
auth_cached_method :login_form_footer

auth_value_methods :login_return_to_requested_location_path
auth_methods :login_response

auth_private_methods :login_form_footer_links

Expand Down Expand Up @@ -77,15 +78,22 @@ module Rodauth
end

attr_reader :login_form_header
attr_reader :saved_login_redirect
private :saved_login_redirect


def login(auth_type)
saved_login_redirect = remove_session_value(login_redirect_session_key)
@saved_login_redirect = remove_session_value(login_redirect_session_key)
transaction do
before_login
login_session(auth_type)
yield if block_given?
after_login
end
login_response
end

def login_response
set_notice_flash login_notice_flash
redirect(saved_login_redirect || login_redirect)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rodauth/features/logout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Rodauth
after
button 'Logout'
redirect{require_login_redirect}
response

auth_methods :logout

Expand All @@ -26,8 +27,7 @@ module Rodauth
logout
after_logout
end
set_notice_flash logout_notice_flash
redirect logout_redirect
logout_response
end
end

Expand Down
10 changes: 5 additions & 5 deletions lib/rodauth/features/otp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ module Rodauth
redirect :otp_disable
redirect :otp_already_setup
redirect :otp_setup
response :otp_disable
response :otp_setup
redirect(:otp_lockout){two_factor_auth_required_redirect}

loaded_templates %w'otp-disable otp-auth otp-setup otp-auth-code-field password-field'
Expand Down Expand Up @@ -182,8 +184,7 @@ module Rodauth
end
after_otp_setup
end
set_notice_flash otp_setup_notice_flash
redirect otp_setup_redirect
otp_setup_response
end

set_error_flash otp_setup_error_flash
Expand All @@ -210,8 +211,7 @@ module Rodauth
end
after_otp_disable
end
set_notice_flash otp_disable_notice_flash
redirect otp_disable_redirect
otp_disable_response
end

set_response_error_reason_status(:invalid_password, invalid_password_error_status)
Expand Down Expand Up @@ -247,7 +247,7 @@ def otp_available?
def otp_exists?
!otp_key.nil?
end

def otp_valid_code?(ot_pass)
if _otp_valid_code?(ot_pass, otp)
true
Expand Down
8 changes: 4 additions & 4 deletions lib/rodauth/features/remember.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Rodauth
after
after 'load_memory'
redirect
response

auth_value_method :raw_remember_token_deadline, nil
auth_value_method :remember_cookie_options, {}.freeze
Expand Down Expand Up @@ -71,15 +72,14 @@ module Rodauth
when remember_remember_param_value
remember_login
when remember_forget_param_value
forget_login
forget_login
when remember_disable_param_value
disable_remember_login
disable_remember_login
end
after_remember
end

set_notice_flash remember_notice_flash
redirect remember_redirect
remember_response
else
set_response_error_reason_status(:invalid_remember_param, invalid_field_error_status)
set_error_flash remember_error_flash
Expand Down
10 changes: 5 additions & 5 deletions lib/rodauth/features/reset_password.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ module Rodauth
redirect
redirect(:reset_password_email_sent){default_post_email_redirect}
redirect(:reset_password_email_recently_sent){default_post_email_redirect}
response
response :reset_password_email_sent
email :reset_password, 'Reset Password'

auth_value_method :reset_password_deadline_column, :deadline
auth_value_method :reset_password_deadline_interval, {:days=>1}.freeze
auth_value_method :reset_password_key_param, 'key'
Expand Down Expand Up @@ -88,8 +90,7 @@ module Rodauth
after_reset_password_request
end

set_notice_flash reset_password_email_sent_notice_flash
redirect reset_password_email_sent_redirect
reset_password_email_sent_response
end

set_error_flash reset_password_request_error_flash
Expand Down Expand Up @@ -154,8 +155,7 @@ module Rodauth
end

remove_session_value(reset_password_session_key)
set_notice_flash reset_password_notice_flash
redirect reset_password_redirect
reset_password_response
end

set_error_flash reset_password_error_flash
Expand Down
10 changes: 5 additions & 5 deletions lib/rodauth/features/sms_codes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ module Rodauth
redirect(:sms_needs_setup){sms_setup_path}
redirect(:sms_request){sms_request_path}
redirect(:sms_lockout){two_factor_auth_required_redirect}
response :sms_confirm
response :sms_disable

loaded_templates %w'sms-auth sms-confirm sms-disable sms-request sms-setup sms-code-field password-field'
view 'sms-auth', 'Authenticate via SMS Code', 'sms_auth'
Expand Down Expand Up @@ -136,7 +138,7 @@ module Rodauth
sms_send_auth_code
after_sms_request
end

set_notice_flash sms_request_notice_flash
redirect sms_auth_redirect
end
Expand Down Expand Up @@ -256,8 +258,7 @@ module Rodauth
end
end

set_notice_flash sms_confirm_notice_flash
redirect sms_confirm_redirect
sms_confirm_response
end

sms_confirm_failure
Expand Down Expand Up @@ -287,8 +288,7 @@ module Rodauth
end
after_sms_disable
end
set_notice_flash sms_disable_notice_flash
redirect sms_disable_redirect
sms_disable_response
end

set_response_error_reason_status(:invalid_password, invalid_password_error_status)
Expand Down
5 changes: 3 additions & 2 deletions lib/rodauth/features/two_factor_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ module Rodauth
redirect(:two_factor_need_setup){two_factor_manage_path}
redirect(:two_factor_auth_required){two_factor_auth_path}

response :two_factor_disable

notice_flash "You have been multifactor authenticated", "two_factor_auth"
notice_flash "All multifactor authentication methods have been disabled", "two_factor_disable"

Expand Down Expand Up @@ -106,8 +108,7 @@ module Rodauth
_two_factor_remove_all_from_session
after_two_factor_disable
end
set_notice_flash two_factor_disable_notice_flash
redirect two_factor_disable_redirect
two_factor_disable_response
end

set_response_error_reason_status(:invalid_password, invalid_password_error_status)
Expand Down
4 changes: 2 additions & 2 deletions lib/rodauth/features/verify_account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module Rodauth
button 'Verify Account'
button 'Send Verification Email Again', 'verify_account_resend'
redirect
response
redirect(:verify_account_email_sent){default_post_email_redirect}
redirect(:verify_account_email_recently_sent){default_post_email_redirect}
email :verify_account, 'Verify Account'
Expand Down Expand Up @@ -154,8 +155,7 @@ module Rodauth
end

remove_session_value(verify_account_session_key)
set_notice_flash verify_account_notice_flash
redirect verify_account_redirect
verify_account_response
end

set_error_flash verify_account_error_flash
Expand Down
4 changes: 2 additions & 2 deletions lib/rodauth/features/verify_login_change.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module Rodauth
before 'verify_login_change_email'
button 'Verify Login Change'
redirect
response
redirect(:verify_login_change_duplicate_account){require_login_redirect}

auth_value_method :verify_login_change_autologin?, false
Expand Down Expand Up @@ -98,8 +99,7 @@ module Rodauth
end

remove_session_value(verify_login_change_session_key)
set_notice_flash verify_login_change_notice_flash
redirect verify_login_change_redirect
verify_login_change_response
end
end

Expand Down
Loading

0 comments on commit 211e81a

Please sign in to comment.