Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support procs for sign_in_after_reset_password config #5653

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
value = resource_class.sign_in_after_reset_password
value.respond_to?(:call) ? value.call(resource) : value
Comment on lines +56 to +57
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wdyt about renaming the variable to setting or config?

Suggested change
value = resource_class.sign_in_after_reset_password
value.respond_to?(:call) ? value.call(resource) : value
setting = resource_class.sign_in_after_reset_password
setting.respond_to?(:call) ? setting.call(resource) : setting

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
23 changes: 23 additions & 0 deletions test/integration/recoverable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,29 @@ def reset_password(options = {}, &block)
end
end

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

assert warden.authenticated?(:user)
end
end

test 'does not sign in user automatically with proc' do
swap Devise, sign_in_after_reset_password: ->(resource) { false }do
excid3 marked this conversation as resolved.
Show resolved Hide resolved
create_user
request_forgot_password
reset_password

assert_contain 'Your password has been changed successfully.'
assert_not_contain 'You are now signed in.'
assert_equal new_user_session_path, @request.path
assert_not warden.authenticated?(:user)
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