1+ // 예시: /api/auth/[action]/route.js
2+ import { NextResponse } from 'next/server' ;
3+ import axios from 'axios' ;
4+
5+ const API_BASE_URL = 'https://gdgocinha.com/api/auth' ; // 프록시 대상 주소
6+
7+ export async function POST ( req , { params } ) {
8+ const action = params . action ; // 'refresh' or 'logout'
9+ const targetUrl = `${ API_BASE_URL } /${ action } ` ;
10+
11+ try {
12+ const cookies = req . headers . get ( 'cookie' ) || '' ;
13+ const accessToken = req . headers . get ( 'authorization' ) ;
14+
15+ const response = await axios . post (
16+ targetUrl ,
17+ await req . json ( ) ,
18+ {
19+ headers : {
20+ 'Content-Type' : 'application/json' ,
21+ ...( accessToken && { Authorization : accessToken } ) ,
22+ Cookie : cookies ,
23+ } ,
24+ withCredentials : true ,
25+ }
26+ ) ;
27+
28+ const nextResponse = NextResponse . json ( response . data , {
29+ status : response . status ,
30+ } ) ;
31+
32+ const setCookies = response . headers [ 'set-cookie' ] ;
33+ if ( setCookies ) {
34+ setCookies . forEach ( ( cookieStr ) => {
35+ const [ nameValue ] = cookieStr . split ( ';' ) ;
36+ const [ name , value ] = nameValue . split ( '=' ) ;
37+ nextResponse . cookies . set ( name , value , {
38+ path : '/' ,
39+ httpOnly : true ,
40+ secure : process . env . NODE_ENV === 'production' ,
41+ sameSite : 'strict' ,
42+ } ) ;
43+ } ) ;
44+ }
45+
46+ return nextResponse ;
47+ } catch ( error ) {
48+ console . error ( `[AUTH PROXY ERROR] /${ action } ` , error . response ?. data || error . message ) ;
49+ return NextResponse . json (
50+ { error : 'AUTH PROXY ERROR' } ,
51+ { status : error . response ?. status || 500 }
52+ ) ;
53+ }
54+ }
0 commit comments