Skip to content

Commit

Permalink
Support lambdas for sign_in_after_reset_password config
Browse files Browse the repository at this point in the history
  • Loading branch information
excid3 committed Nov 8, 2023
1 parent e2242a9 commit cb65878
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
9 changes: 7 additions & 2 deletions app/controllers/devise/passwords_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def update

if resource.errors.empty?
resource.unlock_access! if unlockable?(resource)
if resource_class.sign_in_after_reset_password
if sign_in_after_reset_password?(resource)
flash_message = resource.active_for_authentication? ? :updated : :updated_not_active
set_flash_message!(:notice, flash_message)
resource.after_database_authentication
Expand All @@ -52,8 +52,13 @@ def update
end

protected
def sign_in_after_reset_password?(resource)
result = resource_class.sign_in_after_reset_password
result.respond_to?(:call) ? result.call(resource) : result
end

def after_resetting_password_path_for(resource)
resource_class.sign_in_after_reset_password ? after_sign_in_path_for(resource) : new_session_path(resource_name)
sign_in_after_reset_password?(resource) ? after_sign_in_path_for(resource) : new_session_path(resource_name)
end

# The path used after sending reset password instructions
Expand Down
13 changes: 13 additions & 0 deletions test/integration/recoverable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,19 @@ def reset_password(options = {}, &block)
end
end

test 'sign in user automatically with proc' do
swap Devise, sign_in_after_reset_password: ->(user) { true } do
swap_model_config User, sign_in_after_reset_password: true do
create_user
request_forgot_password
reset_password

assert warden.authenticated?(:user)
end
end
end


test 'does not sign in user automatically after changing its password if it\'s locked and unlock strategy is :none or :time' do
[:none, :time].each do |strategy|
swap Devise, unlock_strategy: strategy do
Expand Down

0 comments on commit cb65878

Please sign in to comment.