Skip to content

Commit 4a20898

Browse files
committed
optimize auth requests
increases the performance of HTTP requests requiring sanctum authentication
1 parent ba1a43f commit 4a20898

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

app/Models/PersonalAccessToken.php

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,20 @@
33
namespace App\Models;
44

55
use Laravel\Sanctum\PersonalAccessToken as SanctumPersonalAccessToken;
6+
use Illuminate\Database\Eloquent\Casts\Attribute;
67

78
class PersonalAccessToken extends SanctumPersonalAccessToken
89
{
910
/**
10-
* @param array $options
11-
* @return void
11+
* Update the last_used_at field no more than 1 time per minute.
12+
* This change increases the performance of HTTP requests requiring sanctum authentication.
1213
*/
13-
public function save(array $options = []): void
14+
protected function lastUsedAt(): Attribute
1415
{
15-
$changes = $this->getDirty();
16-
17-
/**
18-
* Update the last_used_at field no more than 1 time per minute.
19-
* This change increases the performance of HTTP requests requiring sanctum authentication.
20-
*/
21-
if (
22-
!array_key_exists('last_used_at', $changes) ||
23-
count($changes) > 1 ||
24-
!$this->getOriginal('last_used_at') ||
25-
$this->getOriginal('last_used_at') < now()->parse($changes['last_used_at'])->subMinute()
26-
) {
27-
parent::save();
28-
}
16+
return Attribute::make(
17+
set: fn (string $value) => $this->getOriginal('last_used_at') < now()->parse($value)->subMinute()
18+
? $value
19+
: $this->getOriginal('last_used_at'),
20+
);
2921
}
3022
}

0 commit comments

Comments
 (0)