At user's table I add an column called(level) and now I wanna redirect users with level admin to paneladmin view after login and redirect users with level user to home view.
-
use `if(){}else{}` based on user roles – Abhishek Sharma Feb 03 '20 at 10:43
-
1already solved please check https://stackoverflow.com/questions/42177044/laravel-5-4-redirection-to-custom-url-after-login – Kamlesh Paul Feb 03 '20 at 10:45
2 Answers
Laravel provides loigin redirect functionality out of the box. All you need to do is to modify Laravel's built in redirectIfAuthenticated middleware. So, you would have something like this:
public function handle( $request, Closure $next, $guard = null ) {
if ( Auth::guard( $guard )->check() ) { //check if user is authenthicated
$user = Auth::user();
switch ( $user->level ) {
case 'admin':
return redirect( )->route('admin');
break;
case 'user':
return redirect()->route('user');
break;
default:
return redirect( '/' );
}
}
return $next( $request );
}
So in this example, we're going through authenticated user's roles, to determine which page he should be redirected. This is a basic example of how to handle different roles for users, but it should give you a glimpse on how this stuff works and help you in resolving your issue. You can read more about Laravel's in built auth system on official documentation, where you can find more methods that handle the logic functionality. Hope that this can help you, and lead you in the right direction.
- 3,346
- 2
- 17
- 35
in controller before redirect user you must check level of user and redirect user according to level of user.
if use a default auth in laravel 6 go to this path => your_project/vendor/laravel/framework/src/llluminate/Foundation/Auth/AuthenticatesUsers.php
find protected function authenticated(Request $request, $user) and modify bottom code with your project and put inside authenticated method :
if($user->level == 'admin') {
return redirect()->route('admin.dashboard');
} else if($user->level == 'user'){
return redirect()->route('user.dashboard');
}
for more detail you can see this question : Laravel 5.4 redirection to custom url after login
- 2,348
- 1
- 16
- 36