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

Corbin's avatar

How do I redirect a user on login/registration based on a condition on the authenticated user?

What I'm trying to do is redirect a user if they are a counsellor to one route, if they aren't to another:

SparkServiceProvider.php

public function booted()
{

    if(auth()->user()->is_counsellor){
        Spark::afterLoginRedirectTo('/timeline');
    } else {
        Spark::afterLoginRedirectTo('/thoughts');
    }

}

Error

Trying to get property 'is_counsellor' of non-object

There has to be a way to override/overrule Sparks redirects in a way that takes into consideration conditions on authentication right? I've seen many unanswered threads about this, so I'm pretty confused

0 likes
1 reply
cwarne's avatar

Since PHP evaluates conditionals left to right, you can always add the auth check before the is_counsellor call.

if(auth()->check() && auth()->user()->is_counsellor){

This way guarantees the user() function will return a valid App\User instance before calling is_counsellor.

Please or to participate in this conversation.