Files
sam-react-prod/src/app/api/auth/check/route.ts
byeongcheolryu 85e51b2e2a [feat]: Safari 쿠키 호환성 및 UI/UX 개선
주요 변경사항:
- Safari 쿠키 호환성 개선 (SameSite=Lax, 개발 환경 Secure 제외)
- Sidebar 활성 메뉴 자동 스크롤 기능 추가
- Sidebar 스크롤바 스타일링 (호버 시에만 표시)
- DashboardLayout sticky 포지셔닝 적용
- IE 브라우저 차단 및 안내 페이지 추가
- 메뉴 탐색 로직 개선 (서브메뉴 우선 매칭)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-13 14:32:14 +09:00

137 lines
4.9 KiB
TypeScript

import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
/**
* 🔵 Next.js 내부 API - 인증 상태 확인 (PHP 백엔드 X)
*
* ⚡ 설계 목적:
* - 성능 최적화: 매번 PHP 백엔드 호출 대신 로컬 쿠키만 확인
* - 백엔드 부하 감소: 간단한 인증 확인은 Next.js에서 처리
* - 사용자 경험: 즉시 응답으로 빠른 페이지 전환
*
* 📍 사용 위치:
* - LoginPage.tsx: 이미 로그인된 사용자를 대시보드로 리다이렉트
* - SignupPage.tsx: 이미 로그인된 사용자를 대시보드로 리다이렉트
* - 뒤로가기 시 캐시 문제 방지
*
* 🔄 동작 방식:
* 1. HttpOnly 쿠키에서 access_token, refresh_token 확인
* 2. access_token 있음 → { authenticated: true } 즉시 응답
* 3. refresh_token만 있음 → PHP /api/v1/refresh 호출하여 토큰 갱신
* 4. 둘 다 없음 → { authenticated: false } 응답
*
* ⚠️ 주의:
* - 이 API는 PHP 백엔드에 존재하지 않습니다
* - Next.js 프론트엔드 자체 유틸리티 API입니다
* - 실제 인증 로직은 여전히 PHP 백엔드가 담당합니다
*/
export async function GET(request: NextRequest) {
try {
// Get tokens from HttpOnly cookies
const accessToken = request.cookies.get('access_token')?.value;
const refreshToken = request.cookies.get('refresh_token')?.value;
// No tokens at all - not authenticated
if (!accessToken && !refreshToken) {
return NextResponse.json(
{ error: 'Not authenticated' },
{ status: 401 }
);
}
// Has access token - authenticated
if (accessToken) {
return NextResponse.json(
{ authenticated: true },
{ status: 200 }
);
}
// Only has refresh token - try to refresh
if (refreshToken && !accessToken) {
console.log('🔄 Access token missing, attempting refresh...');
console.log('🔍 Refresh token exists:', refreshToken.substring(0, 20) + '...');
console.log('🔍 Backend URL:', process.env.NEXT_PUBLIC_API_URL);
// Attempt token refresh
try {
const refreshResponse = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/v1/refresh`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-API-KEY': process.env.NEXT_PUBLIC_API_KEY || '',
},
body: JSON.stringify({
refresh_token: refreshToken,
}),
});
console.log('🔍 Refresh API response status:', refreshResponse.status);
if (refreshResponse.ok) {
const data = await refreshResponse.json();
// Set new tokens with Safari-compatible configuration
// Safari compatibility: Secure only in production (HTTPS)
const isProduction = process.env.NODE_ENV === 'production';
const accessTokenCookie = [
`access_token=${data.access_token}`,
'HttpOnly', // ✅ JavaScript cannot access
...(isProduction ? ['Secure'] : []), // ✅ HTTPS only in production (Safari fix)
'SameSite=Lax', // ✅ CSRF protection (Lax for better compatibility)
'Path=/',
`Max-Age=${data.expires_in || 7200}`,
].join('; ');
const refreshTokenCookie = [
`refresh_token=${data.refresh_token}`,
'HttpOnly', // ✅ JavaScript cannot access
...(isProduction ? ['Secure'] : []), // ✅ HTTPS only in production (Safari fix)
'SameSite=Lax', // ✅ CSRF protection (Lax for better compatibility)
'Path=/',
'Max-Age=604800', // 7 days (longer for refresh token)
].join('; ');
console.log('✅ Token auto-refreshed in auth check');
const response = NextResponse.json(
{ authenticated: true, refreshed: true },
{ status: 200 }
);
response.headers.append('Set-Cookie', accessTokenCookie);
response.headers.append('Set-Cookie', refreshTokenCookie);
return response;
} else {
const errorData = await refreshResponse.text();
console.error('❌ Refresh API failed:', refreshResponse.status, errorData);
}
} catch (error) {
console.error('❌ Token refresh failed in auth check:', error);
}
// Refresh failed - not authenticated
console.log('⚠️ Returning 401 due to refresh failure');
return NextResponse.json(
{ error: 'Token refresh failed' },
{ status: 401 }
);
}
// Fallback - not authenticated
return NextResponse.json(
{ error: 'Not authenticated' },
{ status: 401 }
);
} catch (error) {
console.error('Auth check error:', error);
return NextResponse.json(
{ error: 'Internal server error', authenticated: false },
{ status: 500 }
);
}
}