Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lat4732's avatar
Level 12

Change default redirect after login/register

Hello guys, how can I change the default redirectTo page after login/register?

0 likes
26 replies
siangboon's avatar

the home route setting is in RouteServiceProvider.php

    public const HOME = '/home';

or change the $redirectTo value in RegisterController.php or LoginController.php

protected $redirectTo = RouteServiceProvider::HOME;
1 like
tykus's avatar

Which starter kit are you using; Jetstream (Fortify), Breeze or Laravel UI?

lat4732's avatar
Level 12

@tykus Jetstream, and the default route redirecting now is /dashboard.

tykus's avatar

@LarAlex in the config/fortify.php you will see there is a home key which (by default) is mapped to RouteServiceProvider::HOME const. So, as @siangboon mentions above, you can change that const to your preferred destination.

lat4732's avatar
Level 12

@tykus That's working. Right now I need to specify a redirecting route based on database user column value. More sample - when a user with column value 1 logged successfully gets redirected to route('/admin') and when column value is 0 - route('/')

I know my question is not literally for this thread but it's close to it and I don't see any reason opening a new one for that.

tykus's avatar
tykus
Best Answer
Level 104

@LarAlex you can replace the default LoginResponse class with your own implementation; this will require you to swap out the registered singleton, e.g.

// app/Providers/FortifyServiceProvider.php
use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;
use App\Actions\Fortify\Http\Responses\LoginResponse;
// ...
public function boot()
{
    $this->app->singleton(LoginResponseContract::class, LoginResponse::class);
    // ...

As you can see this is registering an App\Actions\Fortify\Http\Responses\LoginResponse.php class in the Container, so you need to create a app/Actions/Fortify/Http/Responses/LoginResponse class:

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 redirect()->intended($request->user()->is_admin ? 'admin' : 'dashboard');
    }
}

As you can see, this is checking the is_admin column on the authenticated User and determining where the user is redirected.

lat4732's avatar
Level 12

@tykus Thats way too messy for me. Isn't there a default location for login redirect that I can add an if statement in there?

tykus's avatar

@LarAlex AFAIK the LoginReponse class is the Fortify approach to achieving this. There is no redirectTo method that you can override.

lat4732's avatar
Level 12

I just noticed there is no app/Actions/Fortify/Http directory, let alone a Responses/LoginResponse. Any idea what is going on?

tykus's avatar

@LarAlex that's correct, there is no directory, but you can create it. This is your app; structure it however you prefer - all I did was make a suggestion about how it could be organized.

lat4732's avatar
Level 12

@tykus Let me tell you that I'm still a beginner. That is way too much for my abilities. I've tried creating the app/Actions/Fortify/Http/Responses/LoginResponse.php with your example but I cannot understand how do I use that in the actual login form? Also the class LoginResponse - implements LoginResponseContract so I guess I need to include the LoginResponseContract somewhere, am I right? You said I can replace the default LoginResponse class with my own implementation but where is actually stored the default LoginResponse and how will that override it? Last thing - what actually is singleton?

tykus's avatar

@LarAlex yes, you need to alias any class(es) (or interface in this case) in your own implementation. Here we alias the Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract:

<?php
namespace App\Actions\Fortify\Http\Responses;

use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;

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($request->user()->is_admin ? 'admin' : 'dashboard');
    }
}

Replacing the default with your own implementation means you are binding your implementation into the Container - this is achieved in a Service Provider:

$this->app->singleton(LoginResponseContract::class, LoginResponse::class);

So whenever the framework resolves a LoginResponseContract out of the Container, it will return your implementation, and not the default.

A singleton is a programming pattern which ensures there is only ever one instance of a class.

lat4732's avatar
Level 12

Actually, I found the easiest way of doing this. In app/config/fortify.php line 64:

'home' => RouteServiceProvider::HOME,

can be changed to an anonymous function and inside this function I can do an if statement with my needs. Just like so:

    'home' => function() {      
        if (auth()->user()->is_admin == 1) {
           return route('admin.home');
        } else {
           return route('site.home');
        }
    },

It is working perfectly for me! Thanks for your help tho!

lat4732's avatar
Level 12

@tykus Thanks for the information. At this point in my app development I need no configuration-caching. Later on I'll upgrade myself and I'll be able to repair that!

tykus's avatar

@LarAlex and then you’ll need to implement a LoginResponse class like I described earlier 👍

lat4732's avatar
Level 12

@tykus Lawl. Finally I got to the point where I need to fix the mistakes I did at the start of the development. So what's the solution for this mess I had build earlier?

config/fortify.php

'home' => function() {
        if (auth()->user()->is_admin == 1) {
           return route('admin.home');
        } elseif(auth()->user()->is_company == 1) {
            return route('view.related.company');
        } else {
            if(auth()->user()->email_verified_at) {
                return route('site.home');
            } else {
                return route('verification.notice');
            }
        }
    },
lat4732's avatar
Level 12

@tykus So I'm doing the following.

Roll back the config/fortify.php.

'home' => RouteServiceProvider::HOME,

Create app/Actions/Fortify/Http/Responses/LoginResponse.php

use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;

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 redirect()->intended($request->user()->is_admin ? 'admin' : 'dashboard');
    }
}

Adding in app/Providers/FortifyServiceProvider.php

// ...
use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;
use App\Actions\Fortify\Http\Responses\LoginResponse;

public function boot() {
        
        $this->app->singleton(LoginResponseContract::class, LoginResponse::class);

        // ....
}

But I'm getting an error

Target class [App\Actions\Fortify\Http\Responses\LoginResponse] does not exist. 

when I try to log in.

kokoshneta's avatar

@Laralex Are you remembering to make sure that your LoginResponse class has namespace App\Actions\Fortify\Http\Responses at the top (and checking that that is indeed the correct namespace)?

Otherwise, you’d be creating the class in the global namespace, and the class won’t be found.

tykus's avatar

@Laralex does the file app/Actions/Fortify/Http/Responses/LoginResponse.php have a namespace?

<?php
namespace App\Actions\Fortify\Http\Responses;

class LoginResponse extends //...

Please or to participate in this conversation.