Reputation: 1985
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
Upvotes: 0
Views: 3607
Reputation: 1656
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);
}
}
Upvotes: 5