What happens to me is that if the user puts the url: project.test/med, the system redirects to Login but then it does not redirect to dashboard but to med. In RedirectIfAuthenticated the expression Auth::guard($guard)->check() is not evaluated or at least in the Log it is not shown, so I am not able to identify what is happening.
/** RedirectIfAuthenticated.php */
public function handle($request, Closure $next, ...$guards)
{
$guards = empty($guards) ? [null] : $guards;
Log::info($request);
Log::info(Auth::user());
foreach ($guards as $guard) {
Log::info(Auth::guard($guard)->check());
Log::info(Auth::check());
if (Auth::guard($guard)->check()) {
return redirect(RouteServiceProvider::HOME);
}
}
Log::info(Auth::check());
Log::info('end');
return $next($request);
}
/** web.php */
Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
return Inertia\Inertia::render('Dashboard');
})->name('dashboard');
Route::middleware(['auth:sanctum','verified'])->get('/med', function (Request $request) {
return Inertia\Inertia::render('Med');
})->name('med');
EDIT: Fortify now ships with a simpler way of doing this. See this answer for simplified way.
The
RedirectIfAuthenticated
middleware does not do what you think it does. This middleware checks to see if a user is authenticated when trying to access a route with theguest
middleware attached to it. So after you login to the application and you try to access/login
route, this middleware will intercept the request and redirect the user to theRouteServiceProvider::HOME
path.With that being said, at the time of this writing, Laravel Fortify does not offer the ability to easily modify where the user gets redirected after registering or logging in to the application. See issue for more info
To get this to work you will have to go through a few steps.
Step 1 - Create new LoginResponse class
Anywhere in your application create a
LoginResponse
class and implementLaravel\Fortify\Contracts\LoginResponse
.Step 2 - Override the default Fortify Login Response
In this step you have to tell Laravel to use your login response instead of the default Fortify response.
Once you're done making the changes the user will be redirected to wherever you set it to be.