4

Railsで認証ジェムを考案します。

「パスワードを忘れた」リンクによるパスワード変更後の自動ログインを防ぐにはどうすればよいですか?

理想的には、「新しいパスワードが保存されました」というメッセージが表示されたページを表示すると便利です。

4

4 に答える 4

4

ここで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' }

そして、それはそれを行う必要があります。

于 2012-05-19T01:15:55.863 に答える
2

これが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

終わり

于 2013-10-22T16:53:20.817 に答える
2

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
于 2019-01-10T12:26:41.113 に答える
0

上記の答えは正解ですが、デバイスのバージョンによって異なります。私は上記のことを実行しましたが、動作させることができませんでした。しばらくすると、resource_paramsメソッドをサポートしていないdeviseバージョンを使用していることがわかりました。その後、そのバージョンで別のバージョンを試し、動作させました。

于 2012-11-22T05:05:07.587 に答える