Laravel Fortify creates a new route, /user/confirm-password
that takes a password input. This route hits the store
method of Laravel\Fortify\Http\Controllers\ConfirmablePasswordController
.
The return in this controller method is as follows:
return $confirmed
? app(PasswordConfirmedResponse::class)
: app(FailedPasswordConfirmationResponse::class);
What I'm not understanding is that both of these classes are empty interfaces. I need it to return JSON in the event the route was hit with an XHR request, so something like this from another Fortify controller method:
return $request->wantsJson()
? new JsonResponse('', 200)
: back()->with('status', 'two-factor-authentication-enabled');
How do I get the JSON response I want from the /user/confirm-password
route?
I managed to figure out what I was missing. Unsure on how exactly I overlooked it but the Customising Redirects section of the Fortify docs covers how to solve this for other contracts.
Adding the following to my FortifyServiceProvider
's register
method was the solution.
$this->app->instance(PasswordConfirmedResponse::class, new class implements PasswordConfirmedResponse {
public function toResponse($request) {
return $request->wantsJson()
? new JsonResponse(['confirmed' => true], 200)
: back()->with('confirmed', true);
}
});
$this->app->instance(FailedPasswordConfirmationResponse::class, new class implements FailedPasswordConfirmationResponse {
public function toResponse($request) {
return $request->wantsJson()
? new JsonResponse(['confirmed' => false], 200)
: back()->with('confirmed', false);
}
});