Skip to content
Advertisement

How can I redirect to different views based on user role or privilege in Laravel 8?

I put the function below in the LoginController class to redirect users to different views, and when after login I got this 419|expired page.

protected function authenticated(Request $request, $user) {
    if ($user->PRIVILEGE == 'C') {
        return redirect()->route('/users');
    } else if ($user->PRIVILEGE == 'B') {
        return redirect('/blog');
    } else {
        return redirect('/');
    }
}

Advertisement

Answer

  1. First put this in your LoginController class: use IlluminateSupportFacadesAuth;

  2. comment out this line protected $redirectTo =… and also add this function in the LoginController class:

    public function redirectPath() {

    if(Auth::user()->privilege =='C'){
    
      return '/users';
    }
    if(Auth::user()->privilege=='B'){
    
      return '/blog';
    }    
    

    }

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement