简体   繁体   中英

Admin login as user in Laravel 5.8

I am working on a Laravel 5.8 project. I want to make an authentication system where the admin can log in as a user. I have separate tables for authentication; one is for admin, and the second is for normal users.

User login works on the default Laravel authentication, and for admin, there is a custom code for login.

Auth::guard('admin')->attempt([
    'name' => $request->name,
    'password' => $request->password
], $request->remember);

For example:

I have 3 users: user1
user2 user3

And one admin user with a username of "admin."

The admin can see a list of users on site. And on that page, there is a button which says "login as...". So when the admin clicks on that button, the admin can log in as that user. This means the admin can do what that user can do. But all work will be done as admin.

In short: If the admin wants to log in as any of the users on front and perform his stuff, the admin will log in as that user. Meanwhile, the admin wants to log in to the front end as User1, but the admin side admin will log in as admin.

Try this instead. You can add 2 table columns, maybe one called "role" with enum values, eg, ADMIN, CUSTOMER, and another called "is_normal_user" with boolean type values, which you can set to true or false. So when you try to login as admin and perform admin functions, use the role by checking if their role is ADMIN or CUSTOMER, then when you try to log in and do customer functions as the admin, use the "is_normal_user" field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM