Ver código fonte

refactor(auth): 简化 refreshToken 方法,移除revokeToken 调用,改为需要自己调用
- 在 verifyToken 方法中添加对 token 过期异常的处理,改为需要自己调用

runphp 6 meses atrás
pai
commit
c4fae81f93
1 arquivos alterados com 7 adições e 4 exclusões
  1. 7 4
      src/Auth.php

+ 7 - 4
src/Auth.php

@@ -3,6 +3,7 @@ declare(strict_types=1);
 
 namespace SixShop\Auth;
 
+use Firebase\JWT\ExpiredException;
 use Firebase\JWT\JWT;
 use Firebase\JWT\Key;
 use Ramsey\Uuid\Uuid;
@@ -28,9 +29,7 @@ class Auth implements AuthInterface
 
     public function refreshToken(string $jwt): string
     {
-        $res = $this->generateToken($this->verifyToken($jwt));
-        $this->revokeToken($jwt);
-        return $res;
+        return $this->generateToken($this->verifyToken($jwt));
     }
 
     public function generateToken(string $userId): string
@@ -52,7 +51,11 @@ class Auth implements AuthInterface
     public function verifyToken(string $jwt): string
     {
         JWT::$leeway = self::SLEEP_WAY;
-        $payload = JWT::decode($jwt, new Key($this->config['jwt_secret'], self::ALGORITHM));
+        try {
+            $payload = JWT::decode($jwt, new Key($this->config['jwt_secret'], self::ALGORITHM));
+        } catch (ExpiredException) {
+            // ... 忽略
+        }
         $res = match (UserTypeEnum::tryFrom($payload->aud)) {
             $this->userType => decrypt_data($payload->sub, $this->config['jwt_secret']),
             default => throw new \Exception('token 类型错误'),