Skip to main content
Tutorial Free

How to Change Redirect After Login/Register in Laravel Breeze

January 23, 2023
3 min read

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

You may also be interested in our related PREMIUM course: Laravel 11: Breeze with User Role Areas

Enjoyed This Tutorial?

Get access to all premium tutorials, video and text courses, and exclusive Laravel resources. Join our community of 10,000+ developers.

Comments & Discussion

B
Basel ✓ Link copied!

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

PK
Povilas Korop ✓ Link copied!

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.

B
Basel ✓ Link copied!

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

PK
Povilas Korop ✓ Link copied!

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?

DS
Debiprasad Sahoo ✓ Link copied!

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

PK
Povilas Korop ✓ Link copied!

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.

VF
Vonso FH ✓ Link copied!

I was using laravel backpack and the route name dashboard is backpack.dashboard and the Laravel not detect any route admin/dashboard like it said on src/Illuminate/Auth/Middleware/RedirectIfAuthenticated.php. So I need to implement this, Thank you laravel daily :)

CL
Caribation Labs ✓ Link copied!

This is for Breeze, I realize, and it looks like work. In laravel/ui we only need one change in HomeController::index:

if( Auth::check()) {
if( Auth::user()->type == 'admin' {
// redirect 1
} elseif (Auth::user()->type == 'student') {
// redirect 2
}
}

plus middleware protection.

A
Abdulrahman ✓ Link copied!

what if i only want to redirect to home page?

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.