Skip to content

Commit

Permalink
Allow customizing invalid token path
Browse files Browse the repository at this point in the history
This commit introduces an 'invalid_token_path_for' method in the
controller helpers that allows customizing the path to which invited
users are redirected to in the event where their invitation token is
invalid.
  • Loading branch information
Lersoo authored and scambra committed Nov 11, 2022
1 parent 779d526 commit 4daf6b3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/controllers/devise/invitations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def has_invitations_left?
def resource_from_invitation_token
unless params[:invitation_token] && self.resource = resource_class.find_by_invitation_token(params[:invitation_token], true)
set_flash_message(:alert, :invitation_token_invalid) if is_flashing_format?
redirect_to after_sign_out_path_for(resource_name)
redirect_to invalid_token_path_for(resource_name)
end
end

Expand Down
4 changes: 4 additions & 0 deletions lib/devise_invitable/controllers/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def after_accept_path_for(resource)
signed_in_root_path(resource)
end

def invalid_token_path_for(resource_name)
after_sign_out_path_for(resource_name)
end

protected

def authenticate_inviter!
Expand Down
10 changes: 10 additions & 0 deletions test/functional/controller_helpers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,14 @@ class ControllerHelpersTest < ActionController::TestCase
assert Devise::InvitationsController.method_defined? :after_accept_path_for
assert !Devise::InvitationsController.instance_methods(false).include?(:after_accept_path_for)
end

test 'invalid token path defaults to after sign out path' do
assert_equal @controller.send(:after_sign_out_path_for, :user), @controller.invalid_token_path_for(:user)
end

test 'invalid token path is customizable from application controller' do
custom_path = 'customized/invalid/token/path'
@controller.instance_eval "def invalid_token_path_for(resource_name) '#{custom_path}' end"
assert_equal @controller.invalid_token_path_for(:user), custom_path
end
end

0 comments on commit 4daf6b3

Please sign in to comment.