Skip to content

Commit 5a8583f

Browse files
committed
Created TokenValidated Event
1 parent 76b2d55 commit 5a8583f

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

src/Events/TokenValidated.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Laravel\Sanctum\Events;
4+
5+
class TokenValidated
6+
{
7+
/**
8+
* The model collection.
9+
*
10+
* @var \Illuminate\Database\Eloquent\Model
11+
*/
12+
public $tokenable;
13+
14+
/**
15+
* Create a new event instance.
16+
*
17+
* @param \Illuminate\Database\Eloquent\Model $tokenable
18+
* @return void
19+
*/
20+
public function __construct($tokenable)
21+
{
22+
$this->tokenable = $tokenable;
23+
}
24+
25+
}

src/Guard.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Contracts\Auth\Factory as AuthFactory;
66
use Illuminate\Http\Request;
77
use Illuminate\Support\Arr;
8+
use Laravel\Sanctum\Events\TokenValidated;
89

910
class Guard
1011
{
@@ -70,6 +71,8 @@ public function __invoke(Request $request)
7071
return;
7172
}
7273

74+
event(new TokenValidated($accessToken->tokenable));
75+
7376
if (method_exists($accessToken->getConnection(), 'hasModifiedRecords') &&
7477
method_exists($accessToken->getConnection(), 'setRecordModificationState')) {
7578
tap($accessToken->getConnection()->hasModifiedRecords(), function ($hasModifiedRecords) use ($accessToken) {

tests/GuardTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
use Illuminate\Contracts\Auth\Factory as AuthFactory;
88
use Illuminate\Database\Eloquent\Model;
99
use Illuminate\Http\Request;
10+
use Illuminate\Support\Facades\Event;
1011
use Illuminate\Support\Str;
1112
use Laravel\Sanctum\Contracts\HasApiTokens as HasApiTokensContract;
13+
use Laravel\Sanctum\Events\TokenValidated;
1214
use Laravel\Sanctum\Guard;
1315
use Laravel\Sanctum\HasApiTokens;
1416
use Laravel\Sanctum\PersonalAccessToken;
@@ -174,6 +176,10 @@ public function test_authentication_with_token_fails_if_user_provider_is_invalid
174176
$factory = $this->app->make(AuthFactory::class);
175177
$requestGuard = $factory->guard('sanctum');
176178

179+
Event::fake([
180+
TokenValidated::class,
181+
]);
182+
177183
$request = Request::create('/', 'GET');
178184
$request->headers->set('Authorization', 'Bearer test');
179185

@@ -195,6 +201,7 @@ public function test_authentication_with_token_fails_if_user_provider_is_invalid
195201

196202
$this->assertNull($returnedUser);
197203
$this->assertInstanceOf(EloquentUserProvider::class, $requestGuard->getProvider());
204+
Event::assertNotDispatched(TokenValidated::class);
198205
}
199206

200207
public function test_authentication_is_successful_with_token_if_user_provider_is_valid()
@@ -208,6 +215,10 @@ public function test_authentication_is_successful_with_token_if_user_provider_is
208215
$factory = $this->app->make(AuthFactory::class);
209216
$requestGuard = $factory->guard('sanctum');
210217

218+
Event::fake([
219+
TokenValidated::class,
220+
]);
221+
211222
$request = Request::create('/', 'GET');
212223
$request->headers->set('Authorization', 'Bearer test');
213224

@@ -229,6 +240,7 @@ public function test_authentication_is_successful_with_token_if_user_provider_is
229240

230241
$this->assertEquals($user->id, $returnedUser->id);
231242
$this->assertInstanceOf(EloquentUserProvider::class, $requestGuard->getProvider());
243+
Event::assertDispatched(TokenValidated::class);
232244
}
233245

234246
public function test_authentication_fails_if_callback_returns_false()

0 commit comments

Comments
 (0)