Skip to content

Commit

Permalink
fix: mock server deployment error
Browse files Browse the repository at this point in the history
  • Loading branch information
vince292007 committed Aug 19, 2024
1 parent 2bab841 commit c4b4564
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions apps/backend-mock/utils/jwt-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ import jwt from 'jsonwebtoken';

import { UserInfo } from './mock-data';

// TODO: Replace with your own secret key
const ACCESS_TOKEN_SECRET = 'access_token_secret';
const REFRESH_TOKEN_SECRET = 'refresh_token_secret';

export interface UserPayload extends UserInfo {
iat: number;
exp: number;
}

export function generateAccessToken(user: UserInfo) {
return jwt.sign(user, process.env.ACCESS_TOKEN_SECRET, { expiresIn: '2h' });
return jwt.sign(user, ACCESS_TOKEN_SECRET, { expiresIn: '2h' });
}

export function generateRefreshToken(user: UserInfo) {
return jwt.sign(user, process.env.REFRESH_TOKEN_SECRET, {
return jwt.sign(user, REFRESH_TOKEN_SECRET, {
expiresIn: '30d',
});
}
Expand All @@ -29,10 +33,7 @@ export function verifyAccessToken(

const token = authHeader.split(' ')[1];
try {
const decoded = jwt.verify(
token,
process.env.ACCESS_TOKEN_SECRET,
) as UserPayload;
const decoded = jwt.verify(token, ACCESS_TOKEN_SECRET) as UserPayload;

const username = decoded.username;
const user = MOCK_USERS.find((item) => item.username === username);
Expand All @@ -47,10 +48,7 @@ export function verifyRefreshToken(
token: string,
): null | Omit<UserInfo, 'password'> {
try {
const decoded = jwt.verify(
token,
process.env.REFRESH_TOKEN_SECRET,
) as UserPayload;
const decoded = jwt.verify(token, REFRESH_TOKEN_SECRET) as UserPayload;
const username = decoded.username;
const user = MOCK_USERS.find((item) => item.username === username);
const { password: _pwd, ...userinfo } = user;
Expand Down

0 comments on commit c4b4564

Please sign in to comment.