Jamie Jamie - 29 days ago 16
PHP Question

Laravel change password reset response

I'm building an API in Laravel 5.3 and I've to change the default response when a password is being reset.

So how would I do this without making changes to the framework. What I want is this:

In my

ResetPasswords
trait located here
\Illuminate\Foundation\Auth\ResetPasswords


The default response is:

/**
* Get the response for a successful password reset.
*
* @param string $response
* @return \Illuminate\Http\Response
*/
protected function sendResetResponse($response)
{
return redirect($this->redirectPath())
->with('status', trans($response));
}

/**
* Get the response for a failed password reset.
*
* @param \Illuminate\Http\Request
* @param string $response
* @return \Illuminate\Http\Response
*/
protected function sendResetFailedResponse(Request $request, $response)
{
return redirect()->back()
->withInput($request->only('email'))
->withErrors(['email' => trans($response)]);
}


What I want is this:

/**
* Get the response for a successful password reset.
*
* @param string $response
* @return \Illuminate\Http\Response
*/
protected function sendResetResponse($response)
{
return response()->json(['success' => trans($response)]);
}

/**
* Get the response for a failed password reset.
*
* @param \Illuminate\Http\Request
* @param string $response
* @return \Illuminate\Http\Response
*/
protected function sendResetFailedResponse(Request $request, $response)
{
return response()->json(['error' => trans($response)], 401);
}


So how can I accomplish that without making changes to the framework?

Answer

Copy the methods in your second codeblock to your ResetPasswordController. This will override the Trait's methods in the controller using it.

By doing so you are not making changes to the Laravel framework and your changes won't be lost on a next composer install.

Comments