Devise logging out automatically after password change

In Devise, if I change user's password and after it gets updated in the db, the site immediately logs out the user. I don't want this behavior - how do i do that. please help.

21301 次浏览

我遇到了同样的问题,下面的代码似乎对我有用。

假设密码控制器是为单一路由设置的。另外,假设经过身份验证的模型是 Account。这样,你就有了以下几点:

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 方法调用,它寻求在帐户中重新签名,但是绕过监督回调并将帐户存储到会话中。

使用可注册模块,它将为您提供注册和编辑用户功能

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

上面的示例在 Devise 中使用多个作用域对我来说不起作用。

我必须在 sign _ in 路径中添加作用域/资源名称才能工作,为了避免混乱,我还必须退出老用户,否则各种各样的混乱将随处可见。

使用上面的示例,我必须进行的更改将类似于下面的内容。

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 的代码更安全。

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

def update
. . . . .<your code>
. . . . .<your code>


sign_in(@user, :bypass => true)


. . . . .<your code>
. . . . .<your code>
end

Use this code to avoid sign out.

sign_in(current_user, :bypass => true)

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

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

config.sign_in_after_reset_password = true

编辑: 截至2020年,默认为 true

比尔 · 艾森豪威尔的最新回答如下:

sign_in(current_account, :bypass => true)已被弃用 改用 bypass_sign_in current_account

更多细节可以在这里找到 http://www.rubydoc.info/github/plataformatec/devise/Devise/Controllers/SignInOut#bypass_sign_in-instance_method

请参考这里的答案,上面的答案我都试过了,没有增加作用域是不行的。 Https://stackoverflow.com/a/30418266/4973585

这不管用 sign_in @user, bypass: true

这个可以 sign_in :user, @user, bypass: true