laravel 5 redirect user after login based on user's role

18,704

Solution 1

I added this function to AuthController.php and everything fixed magically

public function authenticated($request , $user){
    if($user->role=='super_admin'){
        return redirect()->route('admin.dashboard') ;
    }elseif($user->role=='brand_manager'){
        return redirect()->route('brands.dashboard') ;
    }
}

Solution 2

If you are using the Authentication system provided with Laravel you can override the redirectPath method in your Auth\AuthController.

For example, this would redirect a user with role 'admin' to /admin and any other user to /account:

public function redirectPath()
{
    if (\Auth::user()->role == 'admin') {
        return "/admin";
        // or return route('routename');
    }

    return "/account";
    // or return route('routename');
}

You could also use Laravel Authorization (introduced in 5.1.11) to manage role logic.

Solution 3

In laravel 5.7 there is no AuthController.php so you have to go Controllers\Auth\LoginController.php and add the below function,

If the redirect path needs custom generation logic you may define a redirectTo method instead of a redirectTo property

protected function redirectTo()
{

    if($user->role=='super_admin'){
        return '/path1';
    }elseif($user->role=='brand_manager'){
        return '/path2';
    }   

}

Solution 4

You can do this handle the request in the Middleware RedirectIfAuthenticated.php inside the handle function like this:

/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @param  string|null  $guard
 * @return mixed
 */
public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check()) {
        if (Auth::user()->role == 'admin') {
            return redirect('/admin');
        }else{
            return redirect('/');
        }
    }

    return $next($request);
}
Share:
18,704

Related videos on Youtube

Salar
Author by

Salar

Software Engineer, Mostly Web... I love architecture and a good challenge to solve. Interested in Edge of tech and changing the rules of current systems, for better.

Updated on September 15, 2022

Comments

  • Salar
    Salar over 1 year

    My 'users' table has a 'role' column and when users are registered or logged in, I want them to be redirected based on their role column. how can I do that?

  • Salar
    Salar about 8 years
    any redirect in the redirectPath() function will result in this error: ErrorException in Response.php line 337: Header may not contain more than a single header, new line detected
  • SlateEntropy
    SlateEntropy about 8 years
    You do not need to return a redirect, just a string of the path
  • Salar
    Salar almost 8 years
    I want to use named routes, I cant return any string
  • SlateEntropy
    SlateEntropy almost 8 years
    as shown (commented out) in my answer, you can use the route() function to return a named route. The route() function, just like asset() and action() returns the requested URL as a string.
  • 8bitIcon
    8bitIcon almost 5 years
    MIddleware RedirectIfAuthenticated.php is the guest middleware. So everything that is for guest,you can't go there if you are logged in. After you login, everything is protected, every route related to user, except logout. So, It will work if you are logged in and try to visit a page which is protected by guest middleware, redirection will work. But when you login first time, this will not work. Better way is to override redirectTo() function.