Files
hyperf-micro-svc/app/Service/Admin/Login/RefreshService.php
2025-09-13 12:16:13 +08:00

52 lines
1.3 KiB
PHP

<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Admin\Login;
use App\Lib\Jwt\RequestScopedTokenTrait;
use App\Service\Admin\BaseAdminService;
use App\Service\BaseTokenService;
use Hyperf\Di\Annotation\Inject;
use Lcobucci\JWT\Token\RegisteredClaims;
use Lcobucci\JWT\UnencryptedToken;
class RefreshService extends BaseAdminService
{
use RequestScopedTokenTrait;
/**
* @var BaseTokenService
*/
#[Inject]
protected BaseTokenService $tokenService;
/**
* @return array
*/
public function handle(): array
{
return $this->adminReturn->success('success',$this->refreshToken($this->getToken()));
}
/**
* @param UnencryptedToken $token
* @return array<string,int|string>
*/
public function refreshToken(UnencryptedToken $token): array
{
$jwt = $this->tokenService->setJwt('admin')->getJwt();
$jwt->addBlackList($token);
return [
'access_token' => $jwt->builderAccessToken($token->claims()->get(RegisteredClaims::ID))->toString(),
'refresh_token' => $jwt->builderRefreshToken($token->claims()->get(RegisteredClaims::ID))->toString(),
'expire_at' => (int) $jwt->getConfig('ttl', 0),
];
}
}