MotorSignal-App/website/app/api/account/firebase-link/route.ts

26 lines
1.1 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) {
if (!hasValidOrigin(request)) {
return NextResponse.json({ detail: 'Invalid request origin.' }, { status: 403 });
}
const accessToken = (await cookies()).get('motorsignal_access_token')?.value;
if (!accessToken) return NextResponse.json({ detail: 'Sign in required.' }, { status: 401 });
try {
const upstream = await fetch(`${process.env.MOTORSIGNAL_API_BASE_URL ?? 'https://api.motorsignal.app'}/api/v1/auth/firebase/link`, {
method: 'POST',
headers: { authorization: `Bearer ${accessToken}`, 'content-type': 'application/json' },
body: JSON.stringify(await request.json()),
cache: 'no-store',
});
return new NextResponse(await upstream.text(), {
status: upstream.status,
headers: { 'content-type': upstream.headers.get('content-type') ?? 'application/json' },
});
} catch {
return NextResponse.json({ detail: 'Firebase linking is unavailable.' }, { status: 503 });
}
}