Open sts-ryan-holton opened 1 year ago
I am facing same issue
@neetu-mittal For me, interestingly, despite having the default_impersonator_guard
config set to web, I actually had to override it in the impersonate function itself. After looking through the raw code (can't find it documented) it looks like the impersonate feature does optionally take in a guard param, this is what I did:
$newUser = User::find($request->input('switch_to'));
$impersonate = Auth::user()->impersonate($newUser, 'web');
In this context, switch_to
is the ID of the User
that I want to impersonate, Auth::user()
is the currently authenticated user, so here I pass in the user object and then $impersonate
does return true.
Hopefully this helps?
https://github.com/404labfr/laravel-impersonate/issues/188#issuecomment-1969216403
Sanctum/Passport uses other guard session, this only supports default session
, but it could be fixed adding quietLogin
and quietLogout
logic.
Had the same issue.
Resolved it by adding macros for the RequestGuard.
Took long to debug due to unset($e);
on line 124 of all exceptions in the ImpersonateManager
.
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
$this->registerTokenDecoder();
$this->registerGuard();
}
private function registerGuard(): void
{
// add methods to RequestGuard to fix issue in impersonate package https://github.com/404labfr/laravel-impersonate/issues/141
RequestGuard::macro('quietLogin', function ($user) {
$this->setUser($user);
});
RequestGuard::macro('quietLogout', function () {
$this->forgetUser();
});
Auth::extend('azureb2cjwt', function () {
return tap(new RequestGuard(new AzureB2CJwtRequestGuard($this->app->make(AzureB2CTokenDecoder::class)), request()), function ($guard) {
// if the request changes, update the guard. This can happen when using horizon
app()->refresh('request', $guard, 'setRequest');
});
});
}
Hi, I'm using Laravel 10 as a backend to a Nuxt JS front-end. I've installed the package, when I send a request to login to my endpoint's function attached, my
$impersonate
variable returns false, why aren't I being logged in?