62 lines
1.9 KiB
TypeScript
62 lines
1.9 KiB
TypeScript
import { cookies } from 'next/headers';
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
import { hasValidOrigin } from '../../../../../lib/request-origin';
|
|
|
|
export async function POST(request: NextRequest) {
|
|
// 1. Validate request origin
|
|
if (!hasValidOrigin(request)) {
|
|
return NextResponse.json({ detail: 'Invalid request origin.' }, { status: 403 });
|
|
}
|
|
|
|
// 2. Obtain real Operations session cookie
|
|
const cookieStore = await cookies();
|
|
const isProduction = process.env.NODE_ENV === 'production';
|
|
const opsToken = isProduction
|
|
? cookieStore.get('__Host-motorsignal_ops')?.value
|
|
: cookieStore.get('__Host-motorsignal_ops')?.value ??
|
|
cookieStore.get('motorsignal_ops')?.value;
|
|
|
|
// 3. If token present, invoke backend session revocation endpoint
|
|
if (opsToken) {
|
|
const baseUrl = process.env.MOTORSIGNAL_API_BASE_URL ?? 'https://api.motorsignal.app';
|
|
try {
|
|
await fetch(`${baseUrl}/api/v1/ops/sessions/current`, {
|
|
method: 'DELETE',
|
|
headers: {
|
|
authorization: `Bearer ${opsToken}`,
|
|
accept: 'application/json',
|
|
},
|
|
cache: 'no-store',
|
|
});
|
|
// All terminal backend states (204, 200, 401 already expired, 403, 404, 503) proceed to clear local cookie
|
|
} catch {
|
|
// Network failure / backend unavailable: proceed safely to clear local cookie without leaking internals
|
|
}
|
|
}
|
|
|
|
// 4. Clear local Operations cookies (host and non-host dev fallback)
|
|
const response = new NextResponse(null, { status: 204 });
|
|
|
|
response.cookies.set('__Host-motorsignal_ops', '', {
|
|
httpOnly: true,
|
|
secure: true,
|
|
sameSite: 'strict',
|
|
path: '/',
|
|
maxAge: 0,
|
|
expires: new Date(0),
|
|
});
|
|
|
|
response.cookies.set('motorsignal_ops', '', {
|
|
httpOnly: true,
|
|
secure: isProduction,
|
|
sameSite: 'strict',
|
|
path: '/',
|
|
maxAge: 0,
|
|
expires: new Date(0),
|
|
});
|
|
|
|
// Rider consumer session cookies are completely unreferenced and untouched
|
|
|
|
return response;
|
|
}
|