|
1 | 1 | import jwt from 'jsonwebtoken'; |
2 | | -import { JWT_SECRET } from '@/app-constants/constants'; |
3 | 2 |
|
4 | | -const REFRESH_SECRET = process.env.REFRESH_SECRET; |
5 | 3 | const ACCESS_TOKEN_EXPIRES_IN = '1h'; |
6 | 4 | const REFRESH_TOKEN_EXPIRES_IN = '7d'; |
7 | 5 |
|
8 | 6 | // 액세스 토큰 생성 함수 |
9 | 7 | export function generateAccessToken(userId: string, email: string): string { |
10 | | - return jwt.sign({ id: userId, email }, JWT_SECRET, { expiresIn: ACCESS_TOKEN_EXPIRES_IN }); |
| 8 | + const jwtSecret = process.env.JWT_SECRET; |
| 9 | + if (!jwtSecret) { |
| 10 | + throw new Error('JWT_SECRET is not defined'); |
| 11 | + } |
| 12 | + |
| 13 | + return jwt.sign({ id: userId, email }, jwtSecret, { |
| 14 | + expiresIn: ACCESS_TOKEN_EXPIRES_IN, |
| 15 | + }); |
11 | 16 | } |
12 | 17 |
|
13 | 18 | // 리프레시 토큰 생성 함수 |
14 | | -export function generateRefreshToken(userId: string, email: string): { refreshToken: string; refreshExpiresAt: Date } { |
15 | | - const refreshToken = jwt.sign({ id: userId, email }, REFRESH_SECRET, { expiresIn: REFRESH_TOKEN_EXPIRES_IN }); |
| 19 | +export function generateRefreshToken( |
| 20 | + userId: string, |
| 21 | + email: string |
| 22 | +): { refreshToken: string; refreshExpiresAt: Date } { |
| 23 | + const refreshSecret = process.env.REFRESH_SECRET; |
| 24 | + if (!refreshSecret) { |
| 25 | + throw new Error('REFRESH_SECRET is not defined'); |
| 26 | + } |
| 27 | + |
| 28 | + // 리프레시 토큰 생성 |
| 29 | + const refreshToken = jwt.sign({ id: userId, email }, refreshSecret, { |
| 30 | + expiresIn: REFRESH_TOKEN_EXPIRES_IN, |
| 31 | + }); |
16 | 32 |
|
17 | | - // 7일 후의 만료 날짜 계산 |
18 | 33 | const refreshExpiresAt = new Date(); |
19 | 34 | refreshExpiresAt.setDate(refreshExpiresAt.getDate() + 7); |
20 | 35 |
|
|
0 commit comments