Open zack6849 opened 3 years ago
in a working app:
current guard returns: Lab404\Impersonate\Guard\SessionGuard guard returns: Lab404\Impersonate\Guard\SessionGuard
in a broken app:
current guard returns: Lab404\Impersonate\Guard\SessionGuard guard returns: Illuminate\Auth\RequestGuard
So, it seems the second line there, for logging in the new user, returns something else
@MarceauKa is this project still being actively maintained?
The issue appears to be in the way that the take()
and leave()
methods resolve the current app guards:
I managed to get it working by rewriting the methods to use the Auth
facade:
public function take($from, $to, $guardName = null)
{
$this->saveAuthCookieInSession();
try {
$currentGuard = $this->getCurrentAuthGuardName();
session()->put($this->getSessionKey(), $from->getAuthIdentifier());
Auth::guard($currentGuard)
->quietLogout();
Auth::guard($guardName ?? $currentGuard)
->quietLogin($to);
After changing this, both the Auth::user()->impersonate()
and Auth::user()->leaveImpersonation()
methods worked exactly as described, so I'm happy for the moment.
Other than some very specific edge cases, I don't think there are any major implications of resolving for the guard with the facade versus trying to key on the service container's App
instance.
Pull request, or nah?
Actually, if I'd spent just a bit more time troubleshooting, I'd have found that this error is corrected through a far easier approach by overriding the impersonate()
method on the app's User
model, and passing in a default name for the Guard
to be used:
namespace App\Models;
/* ... */
use Lab404\Impersonate\Services\ImpersonateManager;
/* ... */
public function impersonate(User $user, $guardName = 'web')
{
return app(ImpersonateManager::class)->take($this, $user, $guardName);
}
Oh, well. I learned a lot of cool stuff while experimenting.
Hi thanks for this, I'll merge a fix asap
@stephancasas this worked for me. Thanks.
Some error with laravel/sanctum . Overwriting 'impersonate' function with 'web' guard doesn't help....
$this->app['auth']->guard($this->getCurrentAuthGuardName())->quietLogout();
getCurrentAuthGuardName() always return 'api', and get exception: Method Illuminate\Auth\RequestGuard::quietLogout does not exist.
I just found another simple solution.
You can move api
guard below web
, and it work.
I just found another simple solution. You can move
api
guard belowweb
, and it work.
This worked for just 1 time. Other times it just failed again
Seems there's a breaking change anywhere, since the relevant code swallows the exception, i just get kicked back to the login page...
BadMethodCallException Method Illuminate\Auth\RequestGuard::quietLogin does not exist.
src/Services/ImpersonateManager.php:121
$this->app['auth']->guard($guardName)->quietLogin($to);