Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Zalo's avatar
Level 19

Laravel Sanctum - Impersonate User

Hi all!

I'm facing an issue trying to impersonate a user.

In my app, I would love to give an Admin user the ability to login as other users. I'm trying to use auth ()->loginUsingId ($user->id) or auth()->login($user) but that methods doesnt exist in Sanctum's guard.

I can not figure out what is the correct way to implement this functionality. Any tip? Thanks!

0 likes
2 replies
drewdan's avatar

There is a package that enables you to impersonate users:

https://github.com/404labfr/laravel-impersonate

The user guide is pretty good and its easy to use, so maybe consider using this? I have no used sanctum, so I am not sure if this does exactly what you need it too, but I think it probably would :)

Zalo's avatar
Level 19

Hi,

Thanks for your reply!

I tried proposed package, but it doesnt worked for me :(

I tried with (Admin is logged and $user is the target user to impersonate):

auth('sanctum')->user()->impersonate($user);

But user didnt get impersonated. If I test auth()->id() it stills retuns admin id instead of impersonated user's id.

So, I just implemented it by myself, creating a new access token for every impersonation... I dont knows if its a good approach.

Please or to participate in this conversation.