Skip to content

Commit

Permalink
Defer updating of last_used_at
Browse files Browse the repository at this point in the history
  • Loading branch information
FarhadShirmardi committed Dec 23, 2024
1 parent adc4b7d commit 8a3d721
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Guard.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ public function __invoke(Request $request)
if (method_exists($accessToken->getConnection(), 'hasModifiedRecords') &&
method_exists($accessToken->getConnection(), 'setRecordModificationState')) {
tap($accessToken->getConnection()->hasModifiedRecords(), function ($hasModifiedRecords) use ($accessToken) {
$accessToken->forceFill(['last_used_at' => now()])->save();
defer(fn() => $accessToken->forceFill(['last_used_at' => now()])->save())->always();

$accessToken->getConnection()->setRecordModificationState($hasModifiedRecords);
});
} else {
$accessToken->forceFill(['last_used_at' => now()])->save();
defer(fn() => $accessToken->forceFill(['last_used_at' => now()])->save())->always();
}

return $tokenable;
Expand Down
3 changes: 3 additions & 0 deletions tests/Feature/GuardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ public function test_authentication_with_token_succeeds_if_expires_at_not_passed
'expires_at' => now()->addMinutes(60),
]);

$this->withoutDefer();
$returnedUser = $guard->__invoke($request);

$this->assertEquals($user->id, $returnedUser->id);
Expand Down Expand Up @@ -187,6 +188,7 @@ public function test_authentication_is_successful_with_token_if_no_session_prese
'name' => 'Test',
]);

$this->withoutDefer();
$returnedUser = $guard->__invoke($request);

$this->assertEquals($user->id, $returnedUser->id);
Expand Down Expand Up @@ -331,6 +333,7 @@ public function test_authentication_is_successful_with_token_in_custom_header()
return $request->header('X-Auth-Token');
});

$this->withoutDefer();
$returnedUser = $guard->__invoke($request);

$this->assertEquals($user->id, $returnedUser->id);
Expand Down

0 comments on commit 8a3d721

Please sign in to comment.