I want to redirect my user to another route based on their role. I have two protected areas in my application, βadminβ and βdashboardβ. I would like to check if the user is authenticated and then redirect to the intended one, but if the user has a movie editor, he should be redirected to the dashboard, otherwise, if he has a role, the administrator should be redirected to the administration area.
I use the AuthenticatesAndRegistersUsers class in my login. I have this on my custom controller:
protected $redirectTo = '/dashboard';
Therefore, when the user authenticates, he will be redirected to the control panel, but I would like to check if the intended URL is on the route of the administrators group, and if the user has the administrator role, he should be redirected to the administrator area.
I use this middleware to redirect to login:
public function handle($request, Closure $next) { if ($this->auth->guest()) { if ($request->ajax()) { return response('Unauthorized.', 401); } else { return redirect()->guest('auth/login'); } } return $next($request); }
source share