0

Is it possible to redirect users with different user roles to a different page in laravel 5.1?

I have looked into Auth middleware and Auth controller, but found nothing that processes the login request itself.

I have found something about login redirection here Laravel redirect back to original destination after login but i'm not sure where to put the suggested code snippets.

Can someone help me out

Community
  • 1
  • 1
user151496
  • 1,849
  • 24
  • 38
  • Would be a task of recreating/overriding the `AuthController` to handle the login differently, or writing a new middleware to handle redirection of a newly logged in user to the appropriate page based on their role. Question is a little too broad for SO purposes though. – Tim Lewis Aug 24 '16 at 17:54

1 Answers1

5

You can update in RedirectIfAuthenticated Middleware

namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Guard;

class RedirectIfAuthenticated
{
    /**
     * The Guard implementation.
     *
     * @var Guard
     */
    protected $auth;

    /**
     * Create a new filter instance.
     *
     * @param  Guard  $auth
     * @return void
     */
    public function __construct(Guard $auth)
    {
        $this->auth = $auth;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($this->auth->check()) {

            $auth = Auth::user()->roles()->first();

            switch ($auth->role) {
                case 'admin':
                        return  redirect()->route('admin');    
                    break;
                case 'superadmin':
                        return  redirect()->route('superadmin'); 
                    break;
                case 'user':
                        return  redirect()->route('user');  
                    break;

                default:
                    # code...
                    return  redirect()->route('user');  
                    break;
            }   

         }

        return $next($request);
    }
}
Parithiban
  • 1,656
  • 11
  • 16