Creating Password Reset Function without using Lar

2019-08-29 10:07发布

问题:

I'm dealing with Laravel 5.6. I am using JWT Authentication, and I create my own authentication controller.

This is my recover method at AuthController,

public function recover(Request $request)
{
    $user = User::where('email', $request->email)->first();
    if (!$user) {
        $error_message = "Your email address was not found.";
        return response()->json(['success' => false, 'error' => ['email'=> $error_message]], 401);
    }
    try {
        Password::sendResetLink($request->only('email'), function (Message $message) {
            $message->subject('Your Password Reset Link');
        });
    } catch (\Exception $e) {
        $error_message = $e->getMessage();
        return response()->json(['success' => false, 'error' => $error_message], 401);
    }
    return response()->json([
        'success' => true, 'data'=> ['message'=> 'A reset email has been sent! Please check your email.']
    ]);
}

In postman, if I execute the recover method, I get this message

{
    "success": false,
    "error": "Route [password.reset] not defined." 
}

How can I deal with this. thanks!

回答1:

You need to give the route a name, in this case password.reset. In your routes.php (or wherever you've defined them) call the name method:

Route::post('/password/reset', 'AuthController@recover')->name('password.reset');


回答2:

If you haven't run make:auth you don't have the route defined, as the error itself is saying.
Try to define the following route in routes/web.php

Route::post('/pwdreset', 'AuthController@recover')
    ->name('password.reset');