2

I am using Laravel 8 jetstream for authentication. My question is, how can I redirect the user after resetting the password to the custom route? I don't want to redirect the user to the login page. I didn't find the route in all Fortify classes; I am sure it should override.

protected $redirectTo

But I don't know in which file I have to do this change.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Mokhtar Ghaleb
  • 423
  • 13
  • 26

3 Answers3

6

Here’s what I ended up doing to have a redirect back to the login route after a user submits a password reset action:

  • Copy the file SuccessfulPasswordResetLinkRequestResponse.php from \vendor\laravel\fortify\Http\Responses\ to a folder on your project at app\Http\Responses.

  • In your new file SuccessfulPasswordResetLinkRequestResponse.php, change the namespace to:

namespace App\Http\Responses;
  • Open app\Providers\FortifyServiceProvider.php

  • Inside the boot() function add:

public function boot() 
{
    ...

    $this->app->singleton(SuccessfulPasswordResetLinkRequestResponseContract::class, SuccessfulPasswordResetLinkRequestResponse::class);
}
  • In this same FortifyServiceProvider.php file, add the namespaces:
use App\Http\Responses\SuccessfulPasswordResetLinkRequestResponse;
use Laravel\Fortify\Contracts\SuccessfulPasswordResetLinkRequestResponse as SuccessfulPasswordResetLinkRequestResponseContract;
  • In your new SuccessfulPasswordResetLinkRequestResponse.php file, edit the toResponse() function:
    public function toResponse($request)
    {
        return $request->wantsJson()
            ? new JsonResponse(['message' => trans($this->status)], 200)
            : redirect()->route('login')->with('status', trans($this->status));
    }

Here's a helpful link that shows all of the response classes that Fortify uses at the time of this writing: Overriding other Jetstream and Fortify functionality

BillD
  • 161
  • 3
  • 7
0

I had the same issue using jetstream and found the accepted solution is better especially if there are admin and user models each has reset password functionality,

You don't need to edit PasswordResetResponse.php in vendor but simply:

  • copy it to App\Http\Responses
  • then as you can find /vendor/laravel/fortify/routes/routes.php
Route::post('/reset-password', [NewPasswordController::class, 'store'])
->middleware(['guest:' . config('fortify.guard')])
->name('password.update');

It points to /laravel/fortify/src/Http/Controllers/NewPasswordController.php.

Store function has the default PasswordResetResponse.php.

public function store(Request $request): Responsable
    {
        $request->validate([
            'token' => 'required',
            Fortify::email() => 'required|email',
            'password' => 'required',
        ]);

    
        $status = $this->broker()->reset(
            $request->only(Fortify::email(), 'password', 'password_confirmation', 'token'),
            function ($user) use ($request) {
                app(ResetsUserPasswords::class)->reset($user, $request->all());

                app(CompletePasswordReset::class)($this->guard, $user);
            }
        );

        return $status == Password::PASSWORD_RESET
            ? app(PasswordResetResponse::class, ['status' => $status])
            : app(FailedPasswordResetResponse::class, ['status' => $status]);
    }

So only need to change is the namespace in NewPasswordController.php file to point to your PasswordResetResponse.php you've made.

use Laravel\Fortify\Contracts\PasswordResetResponse; 
 To--
use App\Http\Responses\PasswordResetResponse;
ouflak
  • 2,458
  • 10
  • 44
  • 49
Mubarak
  • 1
  • 1
-1

EDIT: It is not recommended to edit a file in vendor, use BillD's solution.

Check out vendor\laravel\fortify\src\Http\Responses\PasswordResetResponse.php

You should be able to modify the response in the method:

/**
     * Create an HTTP response that represents the object.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function toResponse($request)
    {
        return $request->wantsJson()
                    ? new JsonResponse(['message' => trans($this->status)], 200)
                    : redirect()->route('login')->with('status', trans($this->status));
    }
Andrew
  • 1,745
  • 1
  • 21
  • 29