![](/img/trans.png)
[英]Override PasswordsController in Devise to change password when the user is already signed in
[英]Set password for user that is already signed in with Devise
我有以下情況:
用戶可以通過單點登錄在我的Web應用程序中進行注冊 。 在這種情況下,無需設置密碼。 我在用戶表中使用布爾列no_password_set對此進行標記 。
在設置頁面中,當我看到布爾值為true時,我沒有給用戶選擇新密碼的選項,而是放了一個按鈕,該按鈕向他們發送一封帶有設置密碼鏈接的郵件。
為此,我使用了devise : send_reset_password_instructions,並且在電子郵件模板中我稍微更改了文本,因此將“ 忘記密碼”更改為“ 設置密碼” 。
到目前為止,到目前為止還不錯,但是當用戶單擊鏈接時,他們會收到一條消息: 您已經登錄。如何避免這種情況? 我希望用戶只能通過郵件確認密碼后才能選擇密碼。 否則,攻擊者可能會在某人的PC上打開瀏覽器,設置密碼並使用該密碼。
當用戶單擊鏈接以設置密碼時注銷用戶也可以接受...
我找到了一個可行的解決方案。
創建此控制器:
class PasswordsController < Devise::PasswordsController
prepend_before_action :require_no_authentication, only: [:cancel ]
end
然后更改路由文件,如下所示:
devise_for :user, controllers: { passwords: 'passwords' }
希望那會工作...
class Devise::PasswordsController < DeviseController
prepend_before_action :remove_session, only: [:edit]
prepend_before_action :require_no_authentication
private
def remove_session
sign_out resource if user_signed_in?
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.