45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
/**
|
|
* Next.js API Route for Google OAuth callback
|
|
* This proxies the token exchange to the backend
|
|
*/
|
|
import { NextRequest, NextResponse } from "next/server";
|
|
import { getBackendUrl } from "@/lib/backend-url";
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const backendUrl = getBackendUrl();
|
|
const body = await request.json();
|
|
const { code, redirect_uri } = body;
|
|
|
|
if (!code) {
|
|
return NextResponse.json(
|
|
{ error: "Missing authorization code" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Forward to backend
|
|
const backendResponse = await fetch(`${backendUrl}/api/auth/google/token`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({ code, redirect_uri }),
|
|
});
|
|
|
|
const data = await backendResponse.json();
|
|
|
|
if (!backendResponse.ok) {
|
|
return NextResponse.json(data, { status: backendResponse.status });
|
|
}
|
|
|
|
return NextResponse.json(data);
|
|
} catch (error: any) {
|
|
console.error("Token exchange error:", error);
|
|
return NextResponse.json(
|
|
{ error: "Token exchange failed", detail: error.message },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|