From b656bc1cc0010d0ac00f00dbc88b02a8940f8860 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 14 Dec 2021 08:08:53 -0600 Subject: [PATCH] rename file - adjust things --- src/Events/TokenAuthenticated.php | 24 ++++++++++++++++++++++++ src/Events/TokenValidated.php | 24 ------------------------ src/Guard.php | 12 +++++++----- tests/GuardTest.php | 10 +++++----- 4 files changed, 36 insertions(+), 34 deletions(-) create mode 100644 src/Events/TokenAuthenticated.php delete mode 100644 src/Events/TokenValidated.php diff --git a/src/Events/TokenAuthenticated.php b/src/Events/TokenAuthenticated.php new file mode 100644 index 00000000..1166340c --- /dev/null +++ b/src/Events/TokenAuthenticated.php @@ -0,0 +1,24 @@ +token = $token; + } +} diff --git a/src/Events/TokenValidated.php b/src/Events/TokenValidated.php deleted file mode 100644 index 481acaf9..00000000 --- a/src/Events/TokenValidated.php +++ /dev/null @@ -1,24 +0,0 @@ -tokenable = $tokenable; - } -} diff --git a/src/Guard.php b/src/Guard.php index a370cc9b..fadb054a 100644 --- a/src/Guard.php +++ b/src/Guard.php @@ -5,7 +5,7 @@ use Illuminate\Contracts\Auth\Factory as AuthFactory; use Illuminate\Http\Request; use Illuminate\Support\Arr; -use Laravel\Sanctum\Events\TokenValidated; +use Laravel\Sanctum\Events\TokenAuthenticated; class Guard { @@ -71,7 +71,11 @@ public function __invoke(Request $request) return; } - event(new TokenValidated($accessToken->tokenable)); + $tokenable = $accessToken->tokenable->withAccessToken( + $accessToken + ); + + event(new TokenAuthenticated($accessToken)); if (method_exists($accessToken->getConnection(), 'hasModifiedRecords') && method_exists($accessToken->getConnection(), 'setRecordModificationState')) { @@ -84,9 +88,7 @@ public function __invoke(Request $request) $accessToken->forceFill(['last_used_at' => now()])->save(); } - return $accessToken->tokenable->withAccessToken( - $accessToken - ); + return $tokenable; } } diff --git a/tests/GuardTest.php b/tests/GuardTest.php index 116ff0c7..4db9d703 100644 --- a/tests/GuardTest.php +++ b/tests/GuardTest.php @@ -10,7 +10,7 @@ use Illuminate\Support\Facades\Event; use Illuminate\Support\Str; use Laravel\Sanctum\Contracts\HasApiTokens as HasApiTokensContract; -use Laravel\Sanctum\Events\TokenValidated; +use Laravel\Sanctum\Events\TokenAuthenticated; use Laravel\Sanctum\Guard; use Laravel\Sanctum\HasApiTokens; use Laravel\Sanctum\PersonalAccessToken; @@ -177,7 +177,7 @@ public function test_authentication_with_token_fails_if_user_provider_is_invalid $requestGuard = $factory->guard('sanctum'); Event::fake([ - TokenValidated::class, + TokenAuthenticated::class, ]); $request = Request::create('/', 'GET'); @@ -201,7 +201,7 @@ public function test_authentication_with_token_fails_if_user_provider_is_invalid $this->assertNull($returnedUser); $this->assertInstanceOf(EloquentUserProvider::class, $requestGuard->getProvider()); - Event::assertNotDispatched(TokenValidated::class); + Event::assertNotDispatched(TokenAuthenticated::class); } public function test_authentication_is_successful_with_token_if_user_provider_is_valid() @@ -216,7 +216,7 @@ public function test_authentication_is_successful_with_token_if_user_provider_is $requestGuard = $factory->guard('sanctum'); Event::fake([ - TokenValidated::class, + TokenAuthenticated::class, ]); $request = Request::create('/', 'GET'); @@ -240,7 +240,7 @@ public function test_authentication_is_successful_with_token_if_user_provider_is $this->assertEquals($user->id, $returnedUser->id); $this->assertInstanceOf(EloquentUserProvider::class, $requestGuard->getProvider()); - Event::assertDispatched(TokenValidated::class); + Event::assertDispatched(TokenAuthenticated::class); } public function test_authentication_fails_if_callback_returns_false()