From 73685ff12821f7491c506bc78e91b0c917dfa75a Mon Sep 17 00:00:00 2001 From: Robert Conner Date: Tue, 6 Dec 2022 22:34:37 -0500 Subject: [PATCH] Add forgetUser() method to GuardHelpers --- src/Illuminate/Auth/GuardHelpers.php | 10 ++++++++++ src/Illuminate/Support/Facades/Auth.php | 1 + tests/Auth/AuthGuardTest.php | 8 ++++++++ 3 files changed, 19 insertions(+) diff --git a/src/Illuminate/Auth/GuardHelpers.php b/src/Illuminate/Auth/GuardHelpers.php index aa9ebf9ec64a..3410ad215cd6 100644 --- a/src/Illuminate/Auth/GuardHelpers.php +++ b/src/Illuminate/Auth/GuardHelpers.php @@ -95,6 +95,16 @@ public function setUser(AuthenticatableContract $user) return $this; } + /** + * Forget the user. + * + * @return void + */ + public function forgetUser() + { + $this->user = null; + } + /** * Get the user provider used by the guard. * diff --git a/src/Illuminate/Support/Facades/Auth.php b/src/Illuminate/Support/Facades/Auth.php index 148d78655d17..568841d94ced 100755 --- a/src/Illuminate/Support/Facades/Auth.php +++ b/src/Illuminate/Support/Facades/Auth.php @@ -56,6 +56,7 @@ * @method static \Illuminate\Auth\SessionGuard setRequest(\Symfony\Component\HttpFoundation\Request $request) * @method static \Illuminate\Support\Timebox getTimebox() * @method static \Illuminate\Contracts\Auth\Authenticatable authenticate() + * @method static void forgetUser() * @method static \Illuminate\Contracts\Auth\UserProvider getProvider() * @method static void setProvider(\Illuminate\Contracts\Auth\UserProvider $provider) * @method static void macro(string $name, object|callable $macro) diff --git a/tests/Auth/AuthGuardTest.php b/tests/Auth/AuthGuardTest.php index e88795f58e1c..6f107ec1640d 100755 --- a/tests/Auth/AuthGuardTest.php +++ b/tests/Auth/AuthGuardTest.php @@ -588,6 +588,14 @@ public function testLoginOnceFailure() $this->assertFalse($guard->once(['foo'])); } + public function testForgetUserSetsUserToNull() + { + $user = m::mock(Authenticatable::class); + $guard = $this->getGuard()->setUser($user); + $guard->forgetUser(); + $this->assertNull($guard->getUser()); + } + protected function getGuard() { [$session, $provider, $request, $cookie, $timebox] = $this->getMocks();