Skip to content

Commit

Permalink
Add an option to not automatically sign in a user after changing a pa…
Browse files Browse the repository at this point in the history
…ssword
  • Loading branch information
knjko committed Jun 24, 2017
1 parent 71fc5b3 commit 2e72fb2
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 7 deletions.
36 changes: 29 additions & 7 deletions app/controllers/devise/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,8 @@ def update
resource_updated = update_resource(resource, account_update_params)
yield resource if block_given?
if resource_updated
if is_flashing_format?
flash_key = update_needs_confirmation?(resource, prev_unconfirmed_email) ?
:update_needs_confirmation : :updated
set_flash_message :notice, flash_key
end
bypass_sign_in resource, scope: resource_name
set_flash_message_for_update(resource, prev_unconfirmed_email)
sign_in_after_change_password
respond_with resource, location: after_update_path_for(resource)
else
clean_up_passwords resource
Expand Down Expand Up @@ -125,7 +121,7 @@ def after_inactive_sign_up_path_for(resource)
# The default url to be used after updating a resource. You need to overwrite
# this method in your own RegistrationsController.
def after_update_path_for(resource)
signed_in_root_path(resource)
sign_in_after_change_password? ? signed_in_root_path(resource) : new_session_path(resource_name)
end

# Authenticates the current scope and gets the current resource from the session.
Expand All @@ -145,4 +141,30 @@ def account_update_params
def translation_scope
'devise.registrations'
end

private

def set_flash_message_for_update(resource, prev_unconfirmed_email)
return unless is_flashing_format?
flash_key = if update_needs_confirmation?(resource, prev_unconfirmed_email)
:update_needs_confirmation
elsif sign_in_after_change_password?
:updated
else
:updated_not_sign_in
end
set_flash_message :notice, flash_key
end

def sign_in_after_change_password
if sign_in_after_change_password?
bypass_sign_in resource, scope: resource_name
else
sign_out(resource)
end
end

def sign_in_after_change_password?
Devise.sign_in_after_change_password && account_update_params.include?(:password)
end
end
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ en:
signed_up_but_unconfirmed: "A message with a confirmation link has been sent to your email address. Please follow the link to activate your account."
update_needs_confirmation: "You updated your account successfully, but we need to verify your new email address. Please check your email and follow the confirm link to confirm your new email address."
updated: "Your account has been updated successfully."
updated_not_sign_in: "Your password has been changed successfully. please try signing in"
sessions:
signed_in: "Signed in successfully."
signed_out: "Signed out successfully."
Expand Down
4 changes: 4 additions & 0 deletions lib/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ module Test
mattr_accessor :token_generator
@@token_generator = nil

# When set to false, changing a password does not automatically sign in a user
mattr_accessor :sign_in_after_change_password
@@sign_in_after_change_password = true

def self.rails51? # :nodoc:
Rails.gem_version >= Gem::Version.new("5.1.x")
end
Expand Down
2 changes: 2 additions & 0 deletions lib/devise/models/registerable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ module ClassMethods
def new_with_session(params, session)
new(params)
end

Devise::Models.config(self, :sign_in_after_change_password)
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions lib/generators/templates/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,10 @@
# When using OmniAuth, Devise cannot automatically set OmniAuth path,
# so you need to do it manually. For the users scope, it would be:
# config.omniauth_path_prefix = '/my_engine/users/auth'

# ==> Configuration for :registerable

# When set to false, does not sign a user in automatically after their password is
# changed. Defaults to true, so a user is signed in automatically after changing a password.
# config.sign_in_after_change_password = true
end
16 changes: 16 additions & 0 deletions test/integration/registerable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,22 @@ def user_sign_up
assert warden.authenticated?(:user)
end

test 'a signed in user should not still be able to use the website after changing their password if config.sign_in_after_change_password is false' do
swap Devise, sign_in_after_change_password: false do
sign_in_as_user
get edit_user_registration_path

fill_in 'password', with: '1234567890'
fill_in 'password confirmation', with: '1234567890'
fill_in 'current password', with: '12345678'
click_button 'Update'

assert_contain 'Your password has been changed successfully. please try signing in'
assert_equal new_user_session_path, @request.path
assert !warden.authenticated?(:user)
end
end

test 'a signed in user should not change their current user with invalid password' do
sign_in_as_user
get edit_user_registration_path
Expand Down
6 changes: 6 additions & 0 deletions test/rails_app/config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,10 @@
# manager.failure_app = AnotherApp
# manager.default_strategies(scope: :user).unshift :some_external_strategy
# end

# ==> Configuration for :registerable

# When set to false, does not sign a user in automatically after their password is
# changed. Defaults to true, so a user is signed in automatically after changing a password.
# config.sign_in_after_change_password = true
end

0 comments on commit 2e72fb2

Please sign in to comment.