Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Laravel fortify reset password


Laravel fortify reset password


here are the key steps to handle password reset functionality with Laravel Fortify:

1. Customize the Password Reset View:
- Use the `Fortify::resetPasswordView()` method in your `FortifyServiceProvider` to define a custom view for the password reset page.
- This view should include a form that makes a POST request to the `/reset-password` endpoint.

php
use Laravel\Fortify\Fortify;

public function boot()
{
    Fortify::resetPasswordView(function () {
        return view('auth.reset-password');
    });
}

2. Handle the Password Reset Request:
- The `/reset-password` endpoint expects a `password`, `password_confirmation`, and `token` field in the request.
- The `token` field should contain the value of `request()->route('token')`.
- You can customize the password reset logic by modifying the `App\Actions\ResetUserPassword` class.

3. Customize the Password Reset Response:
- If the password reset request is successful, Fortify will redirect the user to the `/login` route and set a `status` session variable.
- You can display the successful status message on the login screen using the `@if (session('status'))` Blade directive.
- For failed requests, the validation errors will be available via the shared `$errors` Blade variable or returned with a 422 HTTP response for XHR requests.

4. Handle the Password Reset Link Request:
- The "forgot password" functionality works by sending an email with a reset link to the user.
- When the user clicks the reset link, they are taken to the password reset view you defined in step 1.
- If the password reset link request was successful, Fortify will redirect the user back to the `/forgot-password` route.

5. Customize the Password Reset Link Request Response:
- You can customize the response for the password reset link request by modifying the logic in the `App\Actions\ResetUserPassword` class.
- If the request was successful, Fortify will redirect the user back to the `/forgot-password` route with a success message in the session.
- For failed requests, the validation errors will be available via the shared `$errors` Blade variable or returned with a 422 HTTP response for XHR requests.

Remember, Fortify is a headless authentication library, so you'll need to handle the frontend implementation of the password reset functionality yourself. The search results provide guidance on how to customize the password reset views and responses to fit your application's needs, including integrating with a Single Page Application (SPA) frontend.

Citations:
[1] https://laracasts.com/discuss/channels/laravel/laravel-fortify-password-reset-login-issue
[2] https://laracasts.com/discuss/channels/laravel/laravel-fortify-post-request-to-reset-password-not-working
[3] https://laravel.com/docs/11.x/fortify
[4] https://www.reddit.com/r/laravel/comments/upwcir/fortify_reset_password_with_spa/
[5] https://www.youtube.com/watch?v=19wLOzhM5sI