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

Armani's avatar
Level 17

Can't change redirectTo conditionally after login

I'm using laravel/ui and everything works fine except I want to redirect user conditionally. I want to redirect user depend on roles, This is my routes:

Auth::routes(['register' => false, 'reset' => false]);

Route::middleware('auth')->group(function(){
    Route::get('/', [HomeController::class, 'index'])->name('home');
});

and this is RouteServiceProvider:

class RouteServiceProvider extends ServiceProvider
{
    public const HOME = '/';

    public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::middleware('api')
                ->prefix('api')
                ->group(base_path('routes/api.php'));

            Route::middleware('web')
                ->group(base_path('routes/web.php'));
        });
    }
    protected function configureRateLimiting()
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
        });
    }
}

and inside LoginController I have these code:

class LoginController extends Controller
{
    use AuthenticatesUsers;

    protected function redirectTo()
    {

        if (auth()->user()->can('dashboard')) {
            return '/';

        }

        return '/documents';

    }

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}

but I cannot make it work

0 likes
10 replies
Sinnbeck's avatar

What exactly happens? Did you try using dd() to check the user?

Also be aware that it will redirect to intended if the user tried to visit a certain page before hitting this :)

Armani's avatar
Level 17

@Sinnbeck I put dd like this:

 protected function redirectTo()
    {

        if (auth()->user()->can('dashboard')) {
            return '/';

        }
		dd('here');
        return '/documents';

    }

and it hits there and shows dd but without dd it always shows home. strangely after removing dd it works fine just for once.

Sinnbeck's avatar

@Armani ?

it always shows home

What does that mean? Is /documents = home ?

Sinnbeck's avatar

@Armani And did you by any chance start by trying to visit / to be redirected to /login. If so, the that is set as your intended route

And how can you get to that route if it is only allowed if you can see the dashboard?

Armani's avatar
Level 17

@Sinnbeck Even if I set public const HOME = '/' inside RouteServiceProvider to '/documents' nothing changes.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@Armani Try this

dd(session()->get('url.intended'));`

If this returns /, then that means it has already been set to use /, as you tried to visit this before /login (as I said above)

1 like
Sinnbeck's avatar

You can do a trick like abort(redirect('/documents')) to force redirect

Armani's avatar
Level 17

@Sinnbeck I set authenticated like this:

public function authenticated($request,$user)
    {
        if (auth()->user()->can('dashboard')) {
            return redirect('/');

        }

        return redirect('/documents');
    }

and it worked.

Please or to participate in this conversation.