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

jaurenigel's avatar

Redirect After Login With User Roles

Hello guys.

I am trying to redirect a user after login. I want a user employee to be redirected to /home and user admin to /admin. Only one user is redirected and the other is showing 401 Unauthorized and the url is still /login.

public function redirectTo(){

    if (Auth::user()->authorizeRoles('employee') == true) {
       return '/home';
    }else if (Auth::user()->authorizeRoles('admin') == true) {
       return view('admin');
    }else {
      return 'login';
    }
0 likes
5 replies
JohnBraun's avatar

Return your routes wrapped in the redirect() method. And since you're returning early, you can clean the code up by removing the unnecessary else statements.

public function redirectTo()
{
    if (Auth::user()->authorizeRoles('employee') == true) {
       return redirect('/home');
    }
    
    if (Auth::user()->authorizeRoles('admin') == true) {
       return view('admin');
    }

    return redirect('/login');
}
JohnBraun's avatar

@JAURENIGEL - Which routes are not working? Notice that you are returning a view() for your 'admin' and a redirect() for your employees. Is that intentional?

jaurenigel's avatar

I changed all. I am now using redirect()

public function redirectTo(){

    if (Auth::user()->authorizeRoles('employee') == true) {
       return redirect('/home');
    }else if (Auth::user()->authorizeRoles('admin') == true) {
       return redirect('/admin');
    }else {
      return redirect('/login');
    }
JohnBraun's avatar
Level 33

@JAURENIGEL - Is your problem solved now, or is there some route still not working?

As I said, you can refactor the code leaving out the unnecessary else statements

public function redirectTo()
{
    if (Auth::user()->authorizeRoles('employee') == true) {
       return redirect('/home');
    }
    
    if (Auth::user()->authorizeRoles('admin') == true) {
       return redirect('admin');
    }

    return redirect('/login');
}

Please or to participate in this conversation.