密码更改后设计身份验证日志

时间:2012-05-18 18:39:52

标签: ruby-on-rails login devise logout password-recovery

在Rails中设计身份验证gem。

如何防止密码更改后自动登录"忘记密码"链路?

理想情况下,显示包含消息&#34的页面会很好;新密码已保存并且#34;。

4 个答案:

答案 0 :(得分:4)

您需要覆盖Devise的passwords_controller,您可以看到here的默认方法。首先,创建自己的控制器,它将继承自Devise控制器:

class User::PasswordsController < Devise::PasswordsController

准备好控制器之后,添加您不想覆盖的所有其他方法,只需在其中调用super。这将是neweditcreate方法。另外,请不要忘记添加受保护的after_sending_reset_password_instructions_path_for(resource_name)方法。

您关注覆盖的方法是update操作。

def update
  self.resource = resource_class.reset_password_by_token(resource_params)

  if resource.errors.empty?
    flash_message = resource.active_for_authentication? ? :updated : :updated_not_active
    set_flash_message(:notice, "Your flash message here")
    redirect_to new_user_session_path
  else
    respond_with resource
  end
end

我们在此处更改的是删除要登录用户的行,并重定向到登录页面,然后设置我们的自定义Flash消息。

最后,您必须告诉设计使用您的新控制器,因此在routes.rbdevise_for :users更改为:

devise_for :users, :controllers => { :passwords => 'users/passwords' }

那应该这样做。

答案 1 :(得分:2)

这是基于3.1.1版本的更新

class Users::PasswordsController < Devise::PasswordsController

 def new
   super
 end

 def edit
   super
 end

 def create
   super
 end

 #override this so user isn't signed in after resetting password
 def update
    self.resource = resource_class.reset_password_by_token(resource_params)

    if resource.errors.empty?
      resource.unlock_access! if unlockable?(resource)
      flash_message = resource.active_for_authentication? ? :updated : :updated_not_active
      set_flash_message(:notice, flash_message) if is_navigational_format?

      respond_with resource, :location => after_resetting_password_path_for(resource)
    else
      respond_with resource
    end

  end

protected

   def after_resetting_password_path_for(resource)
     new_session_path(resource)
   end

答案 2 :(得分:0)

上面说的答案是正确的,但事情是它根据设计版本而有所不同。我按照上面的说法,我无法让它工作,一段时间后,我发现我使用的设计版本不支持resource_params方法,然后我尝试了不同的版本,让它工作。

答案 3 :(得分:0)

从设计3.5.0开始,可以通过config/initializers/devise.rb中的设置来控制此行为:

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

显示的即显消息为Your password has been changed successfully.,但可以在config/locales/devise.en.yml中进行调整:

en:
  devise:
    passwords:
      updated_not_active: New password has been saved