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

jason100's avatar

Setting session data and conditional redirect on login

What I want to do, is when a user logs in, pull a UserType value from the Users table, put that value in a session, and redirect the user, depending on $_SESSION[userType].
So, an 'admin' userType would go to one page/route, while a 'guest' userType would go to a different page/route.
would I do something like this in AuthenticatesUsers.php->login()? to pre-empt the "protected $redirectPath =.." ?

0 likes
6 replies
rdelorier's avatar

You can override the redirectPath() function and check the authenticated user there.

Snapey's avatar

You can override the authenticated trait in your AuthController.php. Add something like this. This is lifted straight from one of my projects. You can see how the user object is passed through. You can redirect wherever you want after authentication.

    public function authenticated($request, $user )
    {
        session(['customerId' => $user->customer_id]);
        return redirect()->intended($this->redirectPath());
    }

jason100's avatar

I did this ... is it correct practice?

protected function authenticated($user){
        Session::put('role_id', Auth::user()->role_id);
        if(Session::get('role_id') == '1') {
            return redirect('/page1');
        }
            return redirect('/page2');
    }

for some reason I couldn't get a value $user->user_id, I could only get $user->email and $user->password ... was there a better way to get the value of role_id ?

Snapey's avatar

you need to accept $request and $user as per my example.

you should then be able to just use $user->role_id

jason100's avatar

OK, that works, but just for my education, is the way that I did it (Auth::user()->role_id), previously, wrong? Why?

Please or to participate in this conversation.