How to Change Redirect After Login/Register in Laravel Breeze

Tutorial last revisioned on March 15, 2024 with Laravel 11

In default Laravel 11, there's no hard-coded redirect value.

Laravel itself checks in the Middleware, which is fired if the user is already logged in if Routes with the name dashboard or home exist. If one exists, the user will be redirected to that route. Otherwise, Laravel redirects to the root.

src/Illuminate/Auth/Middleware/RedirectIfAuthenticated.php:

class RedirectIfAuthenticated
{
// ...
 
protected function defaultRedirectUri(): string
{
foreach (['dashboard', 'home'] as $uri) {
if (Route::has($uri)) {
return route($uri);
}
}
 
$routes = Route::getRoutes()->get('GET');
 
foreach (['dashboard', 'home'] as $uri) {
if (isset($routes[$uri])) {
return '/'.$uri;
}
}
 
return '/';
}
}

So, first, you need to have your custom logic.

Let's say that your logic will be inside of your User model, like this:

app/Models/User.php:

class User extends Authenticatable
{
// ...
 
public function getRedirectRoute()
{
return match((int)$this->role_id) {
1 => 'student.dashboard',
2 => 'teacher.dashboard',
// ...
};
}
}

Then, you must create your Middleware with the logic and assign the same guest, in this case, an alias.

php artisan make:middleware OnlyGuestAllowedMiddleware

app/Middleware/OnlyGuestAllowedMiddleware.php:

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;
 
class OnlyGuestAllowedMiddleware
{
public function handle(Request $request, Closure $next, string ...$guards): Response
{
$guards = empty($guards) ? [null] : $guards;
 
foreach ($guards as $guard) {
if (Auth::guard($guard)->check()) {
return redirect(Auth::user()->getRedirectRoute());
}
}
 
return $next($request);
}
}

bootstrap/app.php:

return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
$middleware->alias([
'guest' => \App\Http\Middleware\OnlyGuestAllowedMiddleware::class
]);
})
->withExceptions(function (Exceptions $exceptions) {
//
})->create();

Laravel Breeze

In Laravel Breeze, you must change in two places separately: Controller for the Registration and Login.

app/Http/Controllers/Auth/RegisteredUserController.php:

public function store(Request $request): RedirectResponse
{
// ...
 
Auth::login($user);
 
return redirect(route('dashboard', absolute: false));
}

Change the last line to:

return redirect(auth()->user()->getRedirectRoute());

And then the Login Controller.

app/Http/Controllers/Auth/AuthenticatedSessionController.php:

public function store(LoginRequest $request): RedirectResponse
{
$request->authenticate();
 
$request->session()->regenerate();
 
return redirect()->intended(route('dashboard', absolute: false));
}

Change the last line to:

return redirect()->intended(auth()->user()->getRedirectRoute());
avatar

I applied your code in breeze but it gives me Unhandled match case 0

avatar

Match case? I didn't use any match operation in this example. Maybe your error is in some other parts? Can't really comment without looking at the whole code of your project.

avatar

thank you for your reply You use it in

public function getRedirectRoute() { return match((int)$this->role_id) { 1 => 'student.dashboard', 2 => 'teacher.dashboard', // ... }; }

	and it works fine when login 

but the problem happens when register so the error appear to me

avatar

Oh right, sorry, missed that part. Well, that means that during registration you don't assign role_id correctly and it's set to 0?

avatar

There should be a feature to see the older versions of this post for older versions of Laravel.

avatar

I wish it would be easy to implement. Our current strategy with posts/tutorials is to try to be always on the latest version, as that's what I advice to all Laravel developers with their projects: always update to the latest version.

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 59 courses (1056 lessons, total 42 h 44 min)
  • 78 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent Premium Tutorials