Route [password.reset] not defined in laravel 5.4 in ResetPasswords.php
Asked Answered
T

5

16

I'm getting this error in my custom app:

InvalidArgumentException in UrlGenerator.php line 304: 
Route [password.reset] not defined.

I know laravel provides a password reset functionality out of the box but I want to write my own class and routes.

Here is my routes in web.php

// Password reset link request routes...
Route::get('password/email', 'Auth\PasswordController@getResetEmail');
Route::post('password/email', 'Auth\PasswordController@postResetEmail');

// Password reset routes...
Route::get('password/reset/{token}', 'Auth\PasswordController@getReset');
Route::post('password/reset', 'Auth\PasswordController@postReset');

And this is my PasswordController:

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\PasswordBroker;
use Illuminate\Foundation\Auth\ResetsPasswords;

class PasswordController extends Controller {



    use ResetsPasswords;

    /**
     * Create a new password controller instance.
     *
     * @param  \Illuminate\Contracts\Auth\Guard  $auth
     * @param  \Illuminate\Contracts\Auth\PasswordBroker  $passwords
     * @return void
     */
    public function __construct(Guard $auth, PasswordBroker $passwords)
    {
        $this->auth = $auth;
        $this->passwords = $passwords;
        $this->middleware('guest');
    }
}

This is my ResetPasswords.php trait:

namespace Illuminate\Foundation\Auth;

use Illuminate\Http\Request;
use Illuminate\Mail\Message;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Password;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

trait ResetsPasswords
{
    use RedirectsUsers;

    /**
     * Display the form to request a password reset link.
     *
     * @return \Illuminate\Http\Response
     */
    public function getResetEmail()
    {
        return view('public.auth.password');
    }

    /**
     * Send a reset link to the given user.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postResetEmail(Request $request)
    {
        $this->validate($request, ['email' => 'required|email']);

        $response = Password::sendResetLink($request->only('email'), function (Message $message) {
            $message->subject($this->getEmailSubject());
        });

        switch ($response) {
            case Password::RESET_LINK_SENT:
                return redirect()->back()->with('status', trans($response));

            case Password::INVALID_USER:
                return redirect()->back()->withErrors(['email' => trans($response)]);
        }
    }

    /**
     * Get the e-mail subject line to be used for the reset link email.
     *
     * @return string
     */
    protected function getEmailSubject()
    {
        return property_exists($this, 'subject') ? $this->subject : 'Your Password Reset Link';
    }

    /**
     * Display the password reset view for the given token.
     *
     * @param  string  $token
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function getReset($token = null)
    {
        if (is_null($token)) {
            throw new NotFoundHttpException;
        }

        return view('public.auth.reset')->with('token', $token);
    }

    /**
     * Reset the given user's password.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postReset(Request $request)
    {
        $this->validate($request, [
            'token' => 'required',
            'email' => 'required|email',
            'password' => 'required|confirmed|min:6',
        ]);

        $credentials = $request->only(
            'email', 'password', 'password_confirmation', 'token'
        );

        $response = Password::reset($credentials, function ($user, $password) {
            $this->resetPassword($user, $password);
        });

        switch ($response) {
            case Password::PASSWORD_RESET:
                return redirect($this->redirectPath())->with('status', trans($response));

            default:
                return redirect()->back()
                            ->withInput($request->only('email'))
                            ->withErrors(['email' => trans($response)]);
        }
    }

    /**
     * Reset the given user's password.
     *
     * @param  \Illuminate\Contracts\Auth\CanResetPassword  $user
     * @param  string  $password
     * @return void
     */
    protected function resetPassword($user, $password)
    {
        $user->password = bcrypt($password);

        $user->save();

        Auth::login($user);
    }
}

The problem is when I push the password reset form button it leads to that error.

Any help would be appreciated...

Tani answered 18/5, 2017 at 13:53 Comment(0)
F
23

those routes need a name.

Here the code..

// Password reset link request routes...
Route::get('password/email', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.email');
Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail');

// Password reset routes...
Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.request');
Route::post('password/reset', 'Auth\ResetPasswordController@postReset')->name('password.reset');
Followthrough answered 18/7, 2017 at 4:25 Comment(1)
FWIW, I needed to switch the names for password.reset and password.request. They should be flipped here - otherwise you get an invalid link in your email using a ? for token.Week
P
6

In the laravel 5.4 sendResetLink has only one parameter credentials

So you have to change your method implementation to this:

$response = $this->passwords->sendResetLink($request->only('email'));

Then you have to put callback to User model (or what exactly you have) that has

trait CanResetPassword

This model must have method:

public function sendPasswordResetNotification($token) {
    // do your callback here
}
Proteose answered 18/5, 2017 at 14:25 Comment(0)
W
3

Route

{{ route('password.email') }}

returns /password/reset/{token}

and route

{{ route('password.request') }}

returns /password/reset

Whoever wrote this must have been really tired that night, but I guess it is too late to change the framework at this point

Whitehall answered 29/8, 2018 at 8:10 Comment(0)
T
3

API using fortify & sanctum

If anyone struggles with that when using API, it should use ResetPassword::createUrlUsing() method.

In the AppServiceProvider add:

public function boot()
{
    // Password reset link in email template...
    ResetPassword::createUrlUsing(static function ($notifiable, $token) {
       // Url of the fronted app for resetting password...
       return config('app.app_url').'/reset-password/'.$token;
    });
}
Thaumatrope answered 3/3, 2023 at 23:57 Comment(0)
F
0

If your reset link contains localhost on the url, remember to change the APP_URL on the .env file and then run: php artisan config:clear

Faletti answered 12/2, 2019 at 22:59 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.