404labfr / laravel-impersonate

Laravel Impersonate is a plugin that allows you to authenticate as your users.

Home Page:https://marceau.casals.fr

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Package seems not to work with Sanctum / Laravel 9?

bobmulder opened this issue · 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

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

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.

commented

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

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?

commented

I did put it in a new folder app/Auth

I did put it in a new folder app/Auth

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

commented

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

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?

commented

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?

commented

@tpharaoh Does the Workaround in #162 (comment) work for you?

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.

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.

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

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);

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.

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.

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'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