人気のPHP WEBアプリケーションフレームワークLaravelのTipsを記録していきます

Laravel の パスワードリセット(再設定)時のユーザー検索カラムを増やす

● Laravel の パスワード再設定時のユーザー検索カラムを増やす

Laravelデフォルトでは、 パスワード再設定時にメールアドレスが同じユーザーの最初に検索されたユーザーに対して
パスワード変更が行われます。
これのユーザー検索するためのDB検索カラムを増やして見ましょう。

例として shop_id と email が同じユーザーで検索するように変更して見ましょう。 ( shop_id を追加します)

● shop_id を検索時に追加する

app/Http/Controllers/UserAuth/ResetPasswordController.php を変更します。
( Laravel デフォルトの authの場合は app/Http/Controllers/Auth/ )

    /**
     * オーバーライド用に  (vendor/laravel/framework/src/Illuminate/Foundation/Auth/ResetsPasswords.php) からコピー
     */
    protected function credentials(Request $request)
    {
        // ● shop_id をプラスする
        return $request->only(
            'shop_id', 'email', 'password', 'password_confirmation', 'token'
        );
    }
    /**
     * オーバーライド用に  (vendor/laravel/framework/src/Illuminate/Foundation/Auth/ResetsPasswords.php) からコピー
     * $shop_id は ルーターから受け取る
     */
    public function reset(Request $request, int $shop_id )
    {

        // ● request に shop_id をプラスする
        $request->request->add(['shop_id' => $shop_id]);

        $request->validate($this->rules(), $this->validationErrorMessages());

        $user = \App\User::where("shop_id","=",$shop_id)->where("email","=",$request->email)->first();

        // Here we will attempt to reset the user's password. If it is successful we
        // will update the password on an actual user model and persist it to the
        // database. Otherwise we will parse the error and return the response.
        $response = $this->broker()->reset(
            $this->credentials($request), function ($user, $password) {
                $this->resetPassword($user, $password);
            }
        );

        // If the password was successfully reset, we will redirect the user back to
        // the application's home authenticated view. If there is an error we can
        // redirect them back to where they came from with their error message.
        return $response == Password::PASSWORD_RESET
                    ? $this->sendResetResponse($request, $response)
                    : $this->sendResetFailedResponse($request, $response);
    }

以上です。
簡単ですね。

No.1512
06/19 15:35

edit