How to override login redirects in Laravel Jetstream or Fortify
As of Laravel Fortify v1.11.1, this is what the Laravel\Fortify\Contracts\LoginResponse
looks like:
<?php
namespace Laravel\Fortify\Http\Responses;
use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;
use Laravel\Fortify\Fortify;
class LoginResponse implements LoginResponseContract
{
/**
* Create an HTTP response that represents the object.
*
* @param \Illuminate\Http\Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function toResponse($request)
{
return $request->wantsJson()
? response()->json(['two_factor' => false])
: redirect()->intended(Fortify::redirects('login'));
}
}
The toResponse
method checks if the request wants json or not and responds accordingly. The actual path to be redirected to for when it isn't a json request is handled by Fortify::redirects('login')
.
Taking a quick look at the redirects
static method in Laravel\Fortify
utility class, you will see the following piece of code:
...
/**
* Get a completion redirect path for a specific feature.
*
* @param string $redirect
* @return string
*/
public static function redirects(string $redirect, $default = null)
{
return config('fortify.redirects.'.$redirect) ?? $default ?? config('fortify.home');
}
This means, to arrive the login redirects, all we have to do is make an entry into the config\fortify.php
file with the following piece of code:
'redirects' => [
'login' => '...', // intended path
],