Skip to content

Commit 916f94e

Browse files
Add sign_in_after_reset_password? check hook to passwords controller (#5826)
Extract a couple small duplicate checks into a method, enabling it as a hook that can be overridden if necessary. It's going to be particularly useful on a flow I'm working on / testing out, to avoid having to copy over the whole block of code from the controller to customize it. We have a similar hook on the registration controller for `sign_in_after_change_password?`, which was also moved to protected. While not much practical change, it hopefully shows better the intention that it's a method users can override if they need, similar to a few other methods in controllers. Also move `update_needs_confirmation?` down to private, as this one in particular I don't think we intended to allow overriding, as it has no practical behavior change other than the flash message.
1 parent 1befcb5 commit 916f94e

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
### Unreleased
22

33
* enhancements
4-
* Allow resource class scopes to override the global configuration for `sign_in_after_change_password` behaviour. [#5824](https://github.com/heartcombo/devise/pull/5824)
4+
* Allow resource class scopes to override the global configuration for `sign_in_after_change_password` behaviour. [#5825](https://github.com/heartcombo/devise/pull/5825)
5+
* Add `sign_in_after_reset_password?` check hook to passwords controller, to allow it to be customized by users. [#5826](https://github.com/heartcombo/devise/pull/5826)
56

67
### 5.0.1 - 2026-02-13
78

app/controllers/devise/passwords_controller.rb

Lines changed: 7 additions & 2 deletions
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?
4040
flash_message = resource.active_for_authentication? ? :updated : :updated_not_active
4141
set_flash_message!(:notice, flash_message)
4242
resource.after_database_authentication
@@ -53,7 +53,7 @@ def update
5353

5454
protected
5555
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)
56+
sign_in_after_reset_password? ? after_sign_in_path_for(resource) : new_session_path(resource_name)
5757
end
5858

5959
# The path used after sending reset password instructions
@@ -69,6 +69,11 @@ def assert_reset_token_passed
6969
end
7070
end
7171

72+
# Check if the user should be signed in automatically after resetting the password.
73+
def sign_in_after_reset_password?
74+
resource_class.sign_in_after_reset_password
75+
end
76+
7277
# Check if proper Lockable module methods are present & unlock strategy
7378
# allows to unlock resource on password reset
7479
def unlockable?(resource)

app/controllers/devise/registrations_controller.rb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,6 @@ def cancel
8282

8383
protected
8484

85-
def update_needs_confirmation?(resource, previous)
86-
resource.respond_to?(:pending_reconfirmation?) &&
87-
resource.pending_reconfirmation? &&
88-
previous != resource.unconfirmed_email
89-
end
90-
9185
# By default we want to require a password checks on update.
9286
# You can overwrite this method in your own RegistrationsController.
9387
def update_resource(resource, params)
@@ -133,6 +127,13 @@ def authenticate_scope!
133127
self.resource = send(:"current_#{resource_name}")
134128
end
135129

130+
# Check if the user should be signed in automatically after updating the password.
131+
def sign_in_after_change_password?
132+
return true if account_update_params[:password].blank?
133+
134+
resource_class.sign_in_after_change_password
135+
end
136+
136137
def sign_up_params
137138
devise_parameter_sanitizer.sanitize(:sign_up)
138139
end
@@ -160,9 +161,9 @@ def set_flash_message_for_update(resource, prev_unconfirmed_email)
160161
set_flash_message :notice, flash_key
161162
end
162163

163-
def sign_in_after_change_password?
164-
return true if account_update_params[:password].blank?
165-
166-
resource_class.sign_in_after_change_password
164+
def update_needs_confirmation?(resource, previous)
165+
resource.respond_to?(:pending_reconfirmation?) &&
166+
resource.pending_reconfirmation? &&
167+
previous != resource.unconfirmed_email
167168
end
168169
end

0 commit comments

Comments
 (0)