Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Package seems not to work with Sanctum / Laravel 9? #156

Open
bobmulder opened this issue Apr 6, 2022 · 19 comments
Open

Package seems not to work with Sanctum / Laravel 9? #156

bobmulder opened this issue Apr 6, 2022 · 19 comments

Comments

@bobmulder
Copy link

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

@MarceauKa
Copy link
Member

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

@jonathanoeijoeng
Copy link

jonathanoeijoeng commented Jul 25, 2022

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.

@Arne1303
Copy link

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

@jonathanoeijoeng
Copy link

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?

@Arne1303
Copy link

I did put it in a new folder app/Auth

@jonathanoeijoeng
Copy link

I did put it in a new folder app/Auth

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

@Arne1303
Copy link

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

@jonathanoeijoeng
Copy link

jonathanoeijoeng commented Jul 27, 2022 via email

@jasonadriaan
Copy link

jasonadriaan commented Aug 4, 2022

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?

@tpharaoh
Copy link

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?

@Arne1303
Copy link

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

@jonathanoeijoeng
Copy link

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.

@jasonadriaan
Copy link

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.

@Ceepster14
Copy link

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

@henriquecm
Copy link

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

@Ceepster14
Copy link

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.

@Kovah
Copy link

Kovah commented Nov 20, 2023

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.

@gkimpson
Copy link

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)

@Ceepster14
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

10 participants