Laravel 8 jetstream stack inertia. Redirect to Home after Login, instead of users choice

gioalvaro picture gioalvaro · Oct 1, 2020 · Viewed 11.8k times · Source

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');

Answer

estern76 picture estern76 · Feb 20, 2021

Go to config/fortify.php and modify this line:

'home' => RouteServiceProvider::HOME,

to:

'home' => function(){
    //if you want to go to a specific route
    return route('dashboard');
  
    //or if you have a bunch of redirection options
    if (Auth::user()->hasRole('admin')) {
       return route('admin.dashboard');
    }
    else{
       return route('guest.dashboard');
    }
}