Skip to content

Commit 82a3d23

Browse files
committed
Support lambdas for sign_in_after_reset_password config
1 parent e2242a9 commit 82a3d23

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

app/controllers/devise/passwords_controller.rb

+7-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def update
3636

3737
if resource.errors.empty?
3838
resource.unlock_access! if unlockable?(resource)
39-
if resource_class.sign_in_after_reset_password
39+
if sign_in_after_reset_password?(resource)
4040
flash_message = resource.active_for_authentication? ? :updated : :updated_not_active
4141
set_flash_message!(:notice, flash_message)
4242
resource.after_database_authentication
@@ -52,8 +52,13 @@ def update
5252
end
5353

5454
protected
55+
def sign_in_after_reset_password?(resource)
56+
value = resource_class.sign_in_after_reset_password
57+
value.respond_to?(:call) ? value.call(resource) : value
58+
end
59+
5560
def after_resetting_password_path_for(resource)
56-
resource_class.sign_in_after_reset_password ? after_sign_in_path_for(resource) : new_session_path(resource_name)
61+
sign_in_after_reset_password?(resource) ? after_sign_in_path_for(resource) : new_session_path(resource_name)
5762
end
5863

5964
# The path used after sending reset password instructions

test/integration/recoverable_test.rb

+23
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,29 @@ def reset_password(options = {}, &block)
247247
end
248248
end
249249

250+
test 'sign in user automatically with proc' do
251+
swap Devise, sign_in_after_reset_password: ->(resource) { true } do
252+
create_user
253+
request_forgot_password
254+
reset_password
255+
256+
assert warden.authenticated?(:user)
257+
end
258+
end
259+
260+
test 'does not sign in user automatically with proc' do
261+
swap Devise, sign_in_after_reset_password: ->(resource) { false }do
262+
create_user
263+
request_forgot_password
264+
reset_password
265+
266+
assert_contain 'Your password has been changed successfully.'
267+
assert_not_contain 'You are now signed in.'
268+
assert_equal new_user_session_path, @request.path
269+
assert_not warden.authenticated?(:user)
270+
end
271+
end
272+
250273
test 'does not sign in user automatically after changing its password if it\'s locked and unlock strategy is :none or :time' do
251274
[:none, :time].each do |strategy|
252275
swap Devise, unlock_strategy: strategy do

0 commit comments

Comments
 (0)