2025-11-10 09:38:59 +09:00
|
|
|
import { NextResponse } from 'next/server';
|
|
|
|
|
import type { NextRequest } from 'next/server';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Logout Proxy Route Handler
|
|
|
|
|
*
|
|
|
|
|
* Purpose:
|
|
|
|
|
* - Call PHP backend logout API
|
|
|
|
|
* - Clear HttpOnly cookie
|
|
|
|
|
* - Ensure complete session cleanup
|
|
|
|
|
*/
|
|
|
|
|
export async function POST(request: NextRequest) {
|
|
|
|
|
try {
|
2025-11-10 17:25:56 +09:00
|
|
|
// Get access_token from HttpOnly cookie
|
|
|
|
|
const accessToken = request.cookies.get('access_token')?.value;
|
2025-11-10 09:38:59 +09:00
|
|
|
|
2025-11-10 17:25:56 +09:00
|
|
|
if (accessToken) {
|
2025-11-10 09:38:59 +09:00
|
|
|
// Call PHP backend logout API
|
|
|
|
|
try {
|
|
|
|
|
await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/v1/logout`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
'Accept': 'application/json',
|
2025-11-10 17:25:56 +09:00
|
|
|
'Authorization': `Bearer ${accessToken}`,
|
2025-11-10 09:38:59 +09:00
|
|
|
'X-API-KEY': process.env.NEXT_PUBLIC_API_KEY || '',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
console.log('✅ Backend logout API called successfully');
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.warn('⚠️ Backend logout API failed (continuing with cookie deletion):', error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-10 17:25:56 +09:00
|
|
|
// Clear both HttpOnly cookies
|
|
|
|
|
const clearAccessToken = [
|
|
|
|
|
'access_token=',
|
2025-11-10 09:38:59 +09:00
|
|
|
'HttpOnly',
|
|
|
|
|
'Secure',
|
|
|
|
|
'SameSite=Strict',
|
|
|
|
|
'Path=/',
|
|
|
|
|
'Max-Age=0', // Delete immediately
|
|
|
|
|
].join('; ');
|
|
|
|
|
|
2025-11-10 17:25:56 +09:00
|
|
|
const clearRefreshToken = [
|
|
|
|
|
'refresh_token=',
|
|
|
|
|
'HttpOnly',
|
|
|
|
|
'Secure',
|
|
|
|
|
'SameSite=Strict',
|
|
|
|
|
'Path=/',
|
|
|
|
|
'Max-Age=0', // Delete immediately
|
|
|
|
|
].join('; ');
|
2025-11-10 09:38:59 +09:00
|
|
|
|
2025-11-10 17:25:56 +09:00
|
|
|
console.log('✅ Logout complete - Access & Refresh tokens cleared');
|
|
|
|
|
|
|
|
|
|
const response = NextResponse.json(
|
2025-11-10 09:38:59 +09:00
|
|
|
{ message: 'Logged out successfully' },
|
2025-11-10 17:25:56 +09:00
|
|
|
{ status: 200 }
|
2025-11-10 09:38:59 +09:00
|
|
|
);
|
|
|
|
|
|
2025-11-10 17:25:56 +09:00
|
|
|
response.headers.append('Set-Cookie', clearAccessToken);
|
|
|
|
|
response.headers.append('Set-Cookie', clearRefreshToken);
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
|
2025-11-10 09:38:59 +09:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Logout proxy error:', error);
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: 'Internal server error' },
|
|
|
|
|
{ status: 500 }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|