Railsで認証ジェムを考案します。
「パスワードを忘れた」リンクによるパスワード変更後の自動ログインを防ぐにはどうすればよいですか?
理想的には、「新しいパスワードが保存されました」というメッセージが表示されたページを表示すると便利です。
Railsで認証ジェムを考案します。
「パスワードを忘れた」リンクによるパスワード変更後の自動ログインを防ぐにはどうすればよいですか?
理想的には、「新しいパスワードが保存されました」というメッセージが表示されたページを表示すると便利です。
ここでpasswords_controller
デフォルトのメソッドを確認できるDeviseをオーバーライドする必要があります。まず、Deviseコントローラーから継承する独自のコントローラーを作成します。
class User::PasswordsController < Devise::PasswordsController
コントローラの準備ができたら、オーバーライドしたくない他のすべてのメソッドを追加し、それらの内部でスーパーを呼び出すだけです。これは、、、new
およびedit
メソッドになりcreate
ます。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
ここで変更するのは、サインインページへのリダイレクトを使用してユーザーにサインインする行を削除し、カスタムフラッシュメッセージを設定することだけです。
最後に、新しいコントローラーを使用するようにdeviseに指示する必要があるため、次のようにroutes.rb
変更devise_for :users
します。
devise_for :users, :controllers => { :passwords => 'users/passwords' }
そして、それはそれを行う必要があります。
これがdeviseの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
終わり
Devise 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
上記の答えは正解ですが、デバイスのバージョンによって異なります。私は上記のことを実行しましたが、動作させることができませんでした。しばらくすると、resource_paramsメソッドをサポートしていないdeviseバージョンを使用していることがわかりました。その後、そのバージョンで別のバージョンを試し、動作させました。