简体   繁体   中英

I need to log in to another user from my Admin account without a password

I'm working on a feature that makes it possible for an admin/super-user to select an account from a user list, and then logout from the admin account and immediately log in as the selected user. This must happen without the admin knowing the user's password.

I am using Laravel with Laravel passport alongside Vue, when a user logs in he receives a token with which he can access his role's pages.

I saw that it was possible with Auth::loginById but this does not work with Laravel Passport.

In my AdminController I have the following function that obviously doesn't work yet.

public function shadowLogin($id)
    {
        $user = User::find($id)->first();
        $credentials = [
            'username' => $user->username,
            'password' => ''
        ];

        if (auth()->attempt($username)) {
            $hierarchy = Hierarchy::where('id', auth()->user()->hierarchy_id)->first();

            $token = auth()->user()->createToken('tokenkey', [$hierarchy->hierarchy_name])->accessToken;

            return response()->json(['data' => auth()->user(), 'token' => $token], 200);
        }
    }

I expect to eventually be able to 'switch' the currently stored user in vue to 'switch' to the selected User account, redirecting to the home page and being logged in.

更改会话user_id (或使用用户凭据加载会话数据)。

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