Home > Software engineering >  Multi auth guards, how to have one ui form for all type of user?
Multi auth guards, how to have one ui form for all type of user?

Time:09-04

I have created a multi auth application using guards and create a three login and register pages for users, admins, managers. I have a different users table for every userstrong text. I just need one login and register page for three type of users. How I should do that??

CodePudding user response:

You could do something like:

if (! Auth::guard('user')->attempt($request->only('email', 'password'), $request->boolean('remember'))) {
    // Logged in as user
}

if (! Auth::guard('admin')->attempt($request->only('email', 'password'), $request->boolean('remember'))) {
    // Logged in as admin
}

...

But, you probably don't need different auth guards and a table for each type of user. Instead, the better approach here IMHO would be to use a single users table, and a single auth guard, but then use something like a permissions/roles system (example: spatie/laravel-permission) to differentiate between the user types.

This way you can just have one way to log in. You can split the login form or you can use the same form for all roles. Based on their role you can redirect them to another page where they can see their actions. For example, a separate admin dashboard. You can use different login URLs of course where you only allow users to log in with a certain role. This is a small adaption to the current login flow by just checking on the role as well. You can even create your own controller for it if needed.

You can always use middleware or permission based on the current role of the user to determine where they can go or what they can see.

Multi auth always makes it more complex, while in your case you just have users that log in but have different permissions.

  • Related