54 lines
1.5 KiB
PHP
54 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Jobs\UpdatePersonalAccessToken;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Laravel\Sanctum\PersonalAccessToken;
|
|
|
|
class CustomPersonalAccessToken extends PersonalAccessToken
|
|
{
|
|
public static function findToken($token)
|
|
{
|
|
$token = Cache::remember("PersonalAccessToken::{$token}", 600,
|
|
function () use ($token) {
|
|
return parent::findToken($token) ?? '_null_';
|
|
});
|
|
if ($token === '_null_') {
|
|
return null;
|
|
}
|
|
|
|
return $token;
|
|
}
|
|
|
|
public static function boot()
|
|
{
|
|
parent::boot();
|
|
// When updating, cancel normal update and manually update
|
|
// the table asynchronously every hour.
|
|
static::updating(function (self $personalAccessToken) {
|
|
try {
|
|
Cache::remember('PersonalAccessToken::lastUsgeUpdate', 3600, function () use ($personalAccessToken) {
|
|
dispatch(new UpdatePersonalAccessToken($personalAccessToken, $personalAccessToken->getDirty()));
|
|
|
|
return now();
|
|
});
|
|
} catch (Exception $e) {
|
|
Log::critical($e->getMessage());
|
|
}
|
|
|
|
return false;
|
|
});
|
|
}
|
|
|
|
public function getTokenableAttribute()
|
|
{
|
|
return Cache::remember("PersonalAccessToken::{$this->id}::tokenable", 600,
|
|
function () {
|
|
return parent::tokenable()->first();
|
|
});
|
|
}
|
|
}
|