Skip to content

Commit

Permalink
rename file - adjust things
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 14, 2021
1 parent 3a5c862 commit b656bc1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 34 deletions.
24 changes: 24 additions & 0 deletions src/Events/TokenAuthenticated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Laravel\Sanctum\Events;

class TokenAuthenticated
{
/**
* The personal access token that was authenticated.
*
* @var \Laravel\Sanctum\PersonalAccessToken
*/
public $token;

/**
* Create a new event instance.
*
* @param \Laravel\Sanctum\PersonalAccessToken $token
* @return void
*/
public function __construct($token)
{
$this->token = $token;
}
}
24 changes: 0 additions & 24 deletions src/Events/TokenValidated.php

This file was deleted.

12 changes: 7 additions & 5 deletions src/Guard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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')) {
Expand All @@ -84,9 +88,7 @@ public function __invoke(Request $request)
$accessToken->forceFill(['last_used_at' => now()])->save();
}

return $accessToken->tokenable->withAccessToken(
$accessToken
);
return $tokenable;
}
}

Expand Down
10 changes: 5 additions & 5 deletions tests/GuardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand All @@ -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()
Expand All @@ -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');
Expand All @@ -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()
Expand Down

0 comments on commit b656bc1

Please sign in to comment.