|
| 1 | +import { fetchPublicJson } from '@/utils/fetch'; |
| 2 | +import { NextRequest, NextResponse } from 'next/server'; |
| 3 | + |
| 4 | +const extractCookieValue = ( |
| 5 | + setCookieHeader: string, |
| 6 | + cookieName: string |
| 7 | +): string | null => { |
| 8 | + const cookieMatch = setCookieHeader.match( |
| 9 | + new RegExp(`${cookieName}=([^;]+)`) |
| 10 | + ); |
| 11 | + return cookieMatch ? cookieMatch[1] : null; |
| 12 | +}; |
| 13 | + |
| 14 | +const clearPreviousTokens = (response: NextResponse) => { |
| 15 | + const isProduction = process.env.NODE_ENV === 'production'; |
| 16 | + |
| 17 | + const clearConfig = { |
| 18 | + path: '/', |
| 19 | + maxAge: 0, |
| 20 | + secure: isProduction, |
| 21 | + ...(isProduction ? {} : { domain: 'localhost' }) |
| 22 | + }; |
| 23 | + |
| 24 | + response.cookies.set('Authorization', '', clearConfig); |
| 25 | + response.cookies.set('Authorization-Refresh', '', { |
| 26 | + ...clearConfig, |
| 27 | + httpOnly: true |
| 28 | + }); |
| 29 | +}; |
| 30 | + |
| 31 | +export async function POST(request: NextRequest) { |
| 32 | + try { |
| 33 | + const refreshToken = request.cookies.get('Authorization-Refresh')?.value; |
| 34 | + |
| 35 | + if (!refreshToken) { |
| 36 | + return NextResponse.json( |
| 37 | + { error: 'Refresh token이 없습니다' }, |
| 38 | + { status: 401 } |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + const requestOptions = { |
| 43 | + method: 'POST', |
| 44 | + headers: { |
| 45 | + Cookie: `Authorization-Refresh=${refreshToken}` |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + const response = await fetchPublicJson('/tokens/refresh', requestOptions); |
| 50 | + |
| 51 | + if (!response.ok) { |
| 52 | + return NextResponse.json( |
| 53 | + { error: '토큰 새로고침 실패' }, |
| 54 | + { status: response.status } |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + const newAccessToken = response.headers.get('Authorization'); |
| 59 | + |
| 60 | + const setCookieHeader = response.headers.get('set-cookie'); |
| 61 | + const newRefreshToken = setCookieHeader |
| 62 | + ? extractCookieValue(setCookieHeader, 'Authorization-Refresh') |
| 63 | + : null; |
| 64 | + |
| 65 | + if (!newAccessToken) { |
| 66 | + return NextResponse.json( |
| 67 | + { error: '새로운 토큰을 받을 수 없습니다' }, |
| 68 | + { status: 500 } |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + const successResponse = NextResponse.json( |
| 73 | + { success: true, accessToken: newAccessToken }, |
| 74 | + { status: 200 } |
| 75 | + ); |
| 76 | + |
| 77 | + clearPreviousTokens(successResponse); |
| 78 | + |
| 79 | + const isProduction = process.env.NODE_ENV === 'production'; |
| 80 | + |
| 81 | + const baseCookieOptions = { |
| 82 | + secure: isProduction, |
| 83 | + path: '/', |
| 84 | + sameSite: 'lax' as const, |
| 85 | + ...(isProduction ? {} : { domain: 'localhost' }) |
| 86 | + }; |
| 87 | + |
| 88 | + successResponse.cookies.set('Authorization', newAccessToken, { |
| 89 | + ...baseCookieOptions, |
| 90 | + maxAge: 60 * 60 * 2, |
| 91 | + httpOnly: false |
| 92 | + }); |
| 93 | + |
| 94 | + if (newRefreshToken) { |
| 95 | + successResponse.cookies.set('Authorization-Refresh', newRefreshToken, { |
| 96 | + ...baseCookieOptions, |
| 97 | + maxAge: 60 * 60 * 24 * 7, |
| 98 | + httpOnly: true |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + return successResponse; |
| 103 | + } catch (error) { |
| 104 | + console.error('Refresh Token API Error:', error); |
| 105 | + return NextResponse.json( |
| 106 | + { error: '토큰 새로고침 처리 중 오류 발생' }, |
| 107 | + { status: 500 } |
| 108 | + ); |
| 109 | + } |
| 110 | +} |
0 commit comments