Skip to content

Commit

Permalink
optimize auth requests
Browse files Browse the repository at this point in the history
increases the performance of HTTP requests requiring sanctum authentication
  • Loading branch information
k2so-dev committed Aug 24, 2024
1 parent ba1a43f commit 4a20898
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions app/Models/PersonalAccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,20 @@
namespace App\Models;

use Laravel\Sanctum\PersonalAccessToken as SanctumPersonalAccessToken;
use Illuminate\Database\Eloquent\Casts\Attribute;

class PersonalAccessToken extends SanctumPersonalAccessToken
{
/**
* @param array $options
* @return void
* Update the last_used_at field no more than 1 time per minute.
* This change increases the performance of HTTP requests requiring sanctum authentication.
*/
public function save(array $options = []): void
protected function lastUsedAt(): Attribute
{
$changes = $this->getDirty();

/**
* Update the last_used_at field no more than 1 time per minute.
* This change increases the performance of HTTP requests requiring sanctum authentication.
*/
if (
!array_key_exists('last_used_at', $changes) ||
count($changes) > 1 ||
!$this->getOriginal('last_used_at') ||
$this->getOriginal('last_used_at') < now()->parse($changes['last_used_at'])->subMinute()
) {
parent::save();
}
return Attribute::make(
set: fn (string $value) => $this->getOriginal('last_used_at') < now()->parse($value)->subMinute()
? $value
: $this->getOriginal('last_used_at'),
);
}
}

0 comments on commit 4a20898

Please sign in to comment.