1

I want to customize Laravel Auth. I want to distinguish between admins and users. In the default users table, I added column role with two options: admin and user. According to the manual, I'm defining redirectTo method.

class LoginController extends Controller { use AuthenticatesUsers; /** * Where to redirect users after login. */ /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('guest')->except('logout'); } protected function redirectTo() { if (Auth::user()->role == 'admin') { return 'admin/home'; } else { return '/main'; } } protected function show_admin_homepage() { return view('auth.admin_h'); } protected function show_user_homepage() { return view('auth.user_h'); } } 

Routes

Route::get('main', 'Auth\LoginController@show_user_homepage')->name('show_user_homepage'); Route::get('admin/home', 'Auth\LoginController@show_admin_homepage')->name('show_admin_homepage'); 

The problem is that when I'm logging as a user or as admin, I'm always redirecting to default laravel home view. What I'm doing wrong?

3 Answers 3

2

The default Login Controller is using AuthenticatesUsers trait. In the trait, there is a protected method defined named authenticated override it in the Login Controller.

Below is an implementation of the authenticated method

protected function authenticated($request, $user) { if($user->role == 'admin') { return redirect('/admin/dashboard'); } else if($user->role == 'user') { return redirect('/dashboard'); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

I found the solution. The problem was middleware. Methods displaying admin and user homepages were defined in LoginController where guest middleware is used. by default. For these method auth middleware should be aplied. Thank you all for engagement.
1

You should change redirection in the handle method of the RedirectIfAuthenticated.php file which locate in the App/Http/Middleware

It method looks like following;

public function handle($request, Closure $next, $guard = null) { if (Auth::guard($guard)->check()) { return redirect('/home'); } return $next($request); } 

Comments

0

I would like to distinguish admins and users...

Instead of trying to re-create the wheel and tinkering with the Auth, why not just use something that's already built and easy to use?

Spatie/Laravel-Permission can help you out a ton here.

For example, to create an 'admin' role, you would just do the following:

$role = Role::create(['name' => 'admin']); 

Then give it some permissions:

$permission = Permission::create(['name' => 'super power']); $role->givePermissionTo($permission); 

And assign a role to a User model in your controller

$user->assignRole('admin'); 

From there you can query whether or not a user has a particular role, either in your controller:

if( $user->hasRole('admin') ) { // The user has the admin role return view('admin-page'); } else { // This is a generic user return view('main'); } 

OR in your blade

@role('admin') I took the red pill! @else I took the blue pill.. @endrole 

I suggest this because it's scalable and easy to integrate into your project. As your project continues to grow, if you need to add new permissions to a role it's a single line of code.

1 Comment

Thanks @UkraineInTheMembrane. I heard about this package. I just want to tinker. It turned out that I don't understand manual here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.