Skip to content

Commit a854760

Browse files
authored
refactor(jwt): 更新 JWT 工具函数实现方式 (#6869)
refactor(jwt): 更新 JWT 工具函数实现方式 - 将默认导入 jsonwebtoken 改为解构导入 sign 和 verify 方法
1 parent c7c3904 commit a854760

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

apps/backend-mock/utils/jwt-utils.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { EventHandlerRequest, H3Event } from 'h3';
33
import type { UserInfo } from './mock-data';
44

55
import { getHeader } from 'h3';
6-
import jwt from 'jsonwebtoken';
6+
import { sign, verify } from 'jsonwebtoken';
77

88
import { MOCK_USERS } from './mock-data';
99

@@ -17,11 +17,11 @@ export interface UserPayload extends UserInfo {
1717
}
1818

1919
export function generateAccessToken(user: UserInfo) {
20-
return jwt.sign(user, ACCESS_TOKEN_SECRET, { expiresIn: '7d' });
20+
return sign(user, ACCESS_TOKEN_SECRET, { expiresIn: '7d' });
2121
}
2222

2323
export function generateRefreshToken(user: UserInfo) {
24-
return jwt.sign(user, REFRESH_TOKEN_SECRET, {
24+
return sign(user, REFRESH_TOKEN_SECRET, {
2525
expiresIn: '30d',
2626
});
2727
}
@@ -40,7 +40,7 @@ export function verifyAccessToken(
4040
}
4141
const token = tokenParts[1] as string;
4242
try {
43-
const decoded = jwt.verify(
43+
const decoded = verify(
4444
token,
4545
ACCESS_TOKEN_SECRET,
4646
) as unknown as UserPayload;
@@ -61,7 +61,7 @@ export function verifyRefreshToken(
6161
token: string,
6262
): null | Omit<UserInfo, 'password'> {
6363
try {
64-
const decoded = jwt.verify(token, REFRESH_TOKEN_SECRET) as UserPayload;
64+
const decoded = verify(token, REFRESH_TOKEN_SECRET) as UserPayload;
6565
const username = decoded.username;
6666
const user = MOCK_USERS.find(
6767
(item) => item.username === username,

0 commit comments

Comments
 (0)