From a854760d26ed06dbca1a00fd4593391aae7d2ef8 Mon Sep 17 00:00:00 2001 From: zengmingyong <42780443+zengmingyong@users.noreply.github.com> Date: Mon, 3 Nov 2025 13:08:06 +0800 Subject: [PATCH] =?UTF-8?q?refactor(jwt):=20=E6=9B=B4=E6=96=B0=20JWT=20?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E5=87=BD=E6=95=B0=E5=AE=9E=E7=8E=B0=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=20(#6869)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refactor(jwt): 更新 JWT 工具函数实现方式 - 将默认导入 jsonwebtoken 改为解构导入 sign 和 verify 方法 --- apps/backend-mock/utils/jwt-utils.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/apps/backend-mock/utils/jwt-utils.ts b/apps/backend-mock/utils/jwt-utils.ts index 71858307..f0f74323 100644 --- a/apps/backend-mock/utils/jwt-utils.ts +++ b/apps/backend-mock/utils/jwt-utils.ts @@ -3,7 +3,7 @@ import type { EventHandlerRequest, H3Event } from 'h3'; import type { UserInfo } from './mock-data'; import { getHeader } from 'h3'; -import jwt from 'jsonwebtoken'; +import { sign, verify } from 'jsonwebtoken'; import { MOCK_USERS } from './mock-data'; @@ -17,11 +17,11 @@ export interface UserPayload extends UserInfo { } export function generateAccessToken(user: UserInfo) { - return jwt.sign(user, ACCESS_TOKEN_SECRET, { expiresIn: '7d' }); + return sign(user, ACCESS_TOKEN_SECRET, { expiresIn: '7d' }); } export function generateRefreshToken(user: UserInfo) { - return jwt.sign(user, REFRESH_TOKEN_SECRET, { + return sign(user, REFRESH_TOKEN_SECRET, { expiresIn: '30d', }); } @@ -40,7 +40,7 @@ export function verifyAccessToken( } const token = tokenParts[1] as string; try { - const decoded = jwt.verify( + const decoded = verify( token, ACCESS_TOKEN_SECRET, ) as unknown as UserPayload; @@ -61,7 +61,7 @@ export function verifyRefreshToken( token: string, ): null | Omit { try { - const decoded = jwt.verify(token, REFRESH_TOKEN_SECRET) as UserPayload; + const decoded = verify(token, REFRESH_TOKEN_SECRET) as UserPayload; const username = decoded.username; const user = MOCK_USERS.find( (item) => item.username === username,