laravel-impersonate icon indicating copy to clipboard operation
laravel-impersonate copied to clipboard

Package seems not to work with Sanctum / Laravel 9?

Open bobmulder opened this issue 2 years ago • 19 comments

Hi there guys,

I have some really weird issue implementing this package in a project of mine. This is weird, since I have two other projects with this package working.

The error I am receiving when installing this package is:

InvalidArgumentException : Auth driver [sanctum] for guard [sanctum] is not defined.
 /project/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php:117
 /project/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php:76
 /project/vendor/laravel/sanctum/src/Sanctum.php:68

I have done some digging and found out that the sanctum driver is missing in the customCreators property of AuthManager. When I disable the ImpersonateServiceProvider::registerAuthDriver method, my tests run perfectly, but impersonation doesn't work. I see that the sanctum driver is visible in the customCreators property of AuthManager.

I am not a specialist in the authentication system of Laravel, so I don't have any clue why this is happening.

Do you have any direction for me to dig further?

Regards, Bob

bobmulder avatar Apr 06 '22 06:04 bobmulder

Hello! This is still revelant? Can you give more info?

MarceauKa avatar Jul 23 '22 19:07 MarceauKa

I have similar case but mine always back to login menu when I click impersonate. Using laravel 9.21.6 and jetsream 2.10.

I use this script in livewire component:

public function impersonate(User $user) { auth()->user()->impersonate($user); return redirect()->route('dashboard'); }

and wire:click="impersonate({{ $user->id }}) in the button.

I have another webs impersonate using same laravel but use breeze and it can work as expected.

jonathanoeijoeng avatar Jul 25 '22 07:07 jonathanoeijoeng

I have similar case but mine always back to login menu when I click impersonate.

@jonathanoeijoeng That does sound like #162, you can check if the workaround described there works until a fix is available

Arne1303 avatar Jul 25 '22 14:07 Arne1303

I have similar case but mine always back to login menu when I click impersonate.

@jonathanoeijoeng That does sound like #162, you can check if the workaround described there works until a fix is available

Hi. Thanks so much for the reply. Where I have to put for the session guard?

jonathanoeijoeng avatar Jul 25 '22 22:07 jonathanoeijoeng

I did put it in a new folder app/Auth

Arne1303 avatar Jul 25 '22 23:07 Arne1303

I did put it in a new folder app/Auth

So we put in new file? What is the name file?

jonathanoeijoeng avatar Jul 26 '22 00:07 jonathanoeijoeng

That depends on you're naming convention, if you follow the default it should be SessionGuard.php

Arne1303 avatar Jul 26 '22 09:07 Arne1303

just did as instructed but still logged out after click impersonate button

On Tue, Jul 26, 2022 at 4:10 PM Arne_ @.***> wrote:

That depends on you're naming convention, if you follow the default it should be SessionGuard.php

— Reply to this email directly, view it on GitHub https://github.com/404labfr/laravel-impersonate/issues/156#issuecomment-1195221312, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABVYLICRSQGG24KRAWCKAY3VV6TRBANCNFSM5SU7736A . You are receiving this because you were mentioned.Message ID: @.***>

jonathanoeijoeng avatar Jul 27 '22 13:07 jonathanoeijoeng

I am also having the issue that once you try to impersonate you're just logged out.

What is the official solution to this issue?

jasonadriaan avatar Aug 04 '22 19:08 jasonadriaan

I have similar case but mine always back to login menu when I click impersonate. Using laravel 9.21.6 and jetsream 2.10.

I use this script in livewire component:

public function impersonate(User $user) { auth()->user()->impersonate($user); return redirect()->route('dashboard'); }

and wire:click="impersonate({{ $user->id }}) in the button.

I have another webs impersonate using same laravel but use breeze and it can work as expected.

I have the identical issue. Have you found a solution?

tpharaoh avatar Aug 12 '22 11:08 tpharaoh

@tpharaoh Does the Workaround in https://github.com/404labfr/laravel-impersonate/issues/162#issuecomment-1193132174 work for you?

Arne1303 avatar Aug 13 '22 16:08 Arne1303

I was able to impersonate after delete config('jetstream.auth_session'), in web route. I dont kmow whether it has impact to security or not.

jonathanoeijoeng avatar Aug 13 '22 23:08 jonathanoeijoeng

I ended up just switching out the auth middleware from sanctum to web:

Route::middleware(['auth:sanctum',]) Route::middleware(['auth:web',])

This worked for me.

jasonadriaan avatar Aug 15 '22 08:08 jasonadriaan

Having this issue with a fresh install of Laravel + Jetstream. Sent to login screen after impersonation

Ceepster14 avatar Nov 11 '22 12:11 Ceepster14

I am using a different library because this happened to me too, unfortunately I got the same problem there, but I found a solution. And I think it will probably work here too.

$result = auth()->user()?->impersonate($user);
Auth::guard('sanctum')->setUser($result);

$result is the model of the impersonated user.

for leaving impersonation, I did this:

$impersonate = Auth::user()?->impersonate();
$user = $impersonate->getImpersonator();
$impersonate->leave();

Auth::guard('sanctum')->setUser($user);

henriquecm avatar Dec 08 '22 19:12 henriquecm

I am using a different library because this happened to me too, unfortunately I got the same problem there, but I found a solution. And I think it will probably work here too.

$result = auth()->user()?->impersonate($user);
Auth::guard('sanctum')->setUser($result);

$result is the model of the impersonated user.

for leaving impersonation, I did this:

$impersonate = Auth::user()?->impersonate();
$user = $impersonate->getImpersonator();
$impersonate->leave();

Auth::guard('sanctum')->setUser($user);

Hi Henrique,

Unfortunately I wasn't able to get your fix working, but thanks for the suggestion.

Ceepster14 avatar Dec 09 '22 11:12 Ceepster14

I was able to make it work by using @henriquecm answer. But instead of

Auth::guard('sanctum')->setUser($result);

I used

Auth::guard('sanctum')->setUser($user);

because setUser() does not accept a bool $result but a user.

Kovah avatar Nov 20 '23 12:11 Kovah

I'm confused - there seems to be a few ways for getting this to work with Jetstream - which way is the recommended approach as I also don't seem to be able to get this working with Jetstream (but works fine on my other site that is using Laravel 11.x and Breeze)

gkimpson avatar May 12 '24 07:05 gkimpson

I'm confused - there seems to be a few ways for getting this to work with Jetstream - which way is the recommended approach as I also don't seem to be able to get this working with Jetstream (but works fine on my other site that is using Laravel 11.x and Breeze)

I eventually gave up with this and switched to use https://github.com/OctopyID/LaraPersonate which I was able to get working well in Jetstream

Ceepster14 avatar May 12 '24 16:05 Ceepster14