You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.6 KiB
67 lines
1.6 KiB
import { NextResponse } from "next/server";
|
|
import { cookies } from "next/headers";
|
|
import {
|
|
SESSION_COOKIE_NAME,
|
|
type GatewayBindResponse,
|
|
popiartFetchEnvelope,
|
|
} from "@/lib/popiart-api";
|
|
|
|
export async function POST(request: Request) {
|
|
const cookieStore = await cookies();
|
|
const token = cookieStore.get(SESSION_COOKIE_NAME)?.value;
|
|
|
|
if (!token) {
|
|
return NextResponse.json(
|
|
{
|
|
ok: false,
|
|
error: {
|
|
code: "UNAUTHENTICATED",
|
|
message: "missing local popiart session",
|
|
},
|
|
},
|
|
{ status: 401 },
|
|
);
|
|
}
|
|
|
|
let body: { gateway_user_id?: number; gateway_access_token?: string } = {};
|
|
|
|
try {
|
|
body = (await request.json()) as { gateway_user_id?: number; gateway_access_token?: string };
|
|
} catch {
|
|
body = {};
|
|
}
|
|
|
|
try {
|
|
const { response, payload } = await popiartFetchEnvelope<GatewayBindResponse>("/auth/gateway/bind", {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Bearer ${token}`,
|
|
},
|
|
body: JSON.stringify(body),
|
|
});
|
|
|
|
return NextResponse.json(
|
|
payload ?? {
|
|
ok: false,
|
|
error: {
|
|
code: "SERVER_ERROR",
|
|
message: "invalid response from popiartServer",
|
|
},
|
|
},
|
|
{ status: response.status },
|
|
);
|
|
} catch (error) {
|
|
return NextResponse.json(
|
|
{
|
|
ok: false,
|
|
error: {
|
|
code: "SERVER_ERROR",
|
|
message: "failed to reach popiartServer",
|
|
details: error instanceof Error ? error.message : String(error),
|
|
},
|
|
},
|
|
{ status: 502 },
|
|
);
|
|
}
|
|
}
|
|
|