* 验证令牌是否有效 * @param {string} token - JWT令牌 * @returns {Object|null} 解码后的令牌信息或null
(token)
| 73 | |
| 74 | /** |
| 75 | * 验证令牌是否有效 |
| 76 | * @param {string} token - JWT令牌 |
| 77 | * @returns {Object|null} 解码后的令牌信息或null |
| 78 | */ |
| 79 | validateToken(token) { |
| 80 | try { |
| 81 | if (!token) return null |
| 82 | |
| 83 | const decoded = JwtDecode(token) |
| 84 | if (!decoded || !decoded.exp) { |
| 85 | return null |
| 86 | } |
| 87 | |
| 88 | const now = Math.floor(Date.now() / 1000) |
| 89 | if (decoded.exp <= now) { |
| 90 | return null // 令牌已过期 |
| 91 | } |
| 92 | |
| 93 | return decoded |
| 94 | } catch (error) { |
| 95 | logger.error('令牌验证失败', 'TOKEN', '', error) |
| 96 | return null |
| 97 | } |
no test coverage detected