改变密码后自动注销

在devise中,如果我更改用户的密码,并在数据库中更新后,网站立即注销用户。 我不想要这种行为 – 我该怎么做。 请帮忙。

我有同样的问题,下面的代码似乎为我工作。

假设密码控制器被设置为一个单身路由。 另外,假定经过身份validation的模型是一个帐户。 有了这个,你有以下几点:

def update if current_account.update_with_password(params[:account]) sign_in(current_account, :bypass => true) flash[:notice] = 'Password updated.' redirect_to account_path else render :action => :show end end 

关键的因素是sign_in方法调用,它试图重新login帐户,但绕过监督callback并将帐户存储到会话中。

上面的例子不适合我在Devise中使用多个范围。

我必须在sign_inpath中添加范围/资源名称才能工作,为了防止混乱,我还必须注销旧用户,否则将会出现各种各样的混淆。

使用上面的例子,我必须做的改变看起来像这样。

 def update if current_account.update_with_password(params[:account]) sign_out(current_account) sign_in(:account, current_account, :bypass => true) flash[:notice] = 'Password updated.' redirect_to account_path else render :action => :show end end 

编辑添加:我相信我必须强行注销用户,因为在某些地方,我会覆盖Devise的代码,以防止用户在某些操作中注销。 事后看来; 不是一个好主意! 这个方法好多了! 因为让你自己的控制器比覆盖Devise的代码更安全,除非它是绝对不可避免的。

使用此代码避免注销。

 sign_in(current_user, :bypass => true) 

在您更新用户密码的方法中,将以下代码添加到数据库中更新用户密码之后:

 def update . . . . .<your code> . . . . .<your code> sign_in(@user, :bypass => true) . . . . .<your code> . . . . .<your code> end 

您可以简单地在sign_in_after_reset_password中设置devise.rb

 config.sign_in_after_reset_password = true 

出于某些原因, current_user不等于@user尽pipecurrent_user.id等于@user.id @user 所以我必须使用sign_in(@user, :bypass => true)

使用可注册的模块,它可以让你注册和编辑用户function

https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-password