import { NextRequest } from "next/server"; import { authenticateBridge } from "@/db/bridge"; import { BRIDGE_SESSION_TTL_SECONDS, bridgeReturnUrl, signBridgeToken, } from "@/lib/bridge"; import { errorResponse, jsonResponse } from "@/lib/http"; /** * POST /api/bridge/sessions/:sessionId/exchange * * The login token is deliberately short-lived because it travels in a URL. * The editor exchanges it once for a service token it keeps server-side; that * token authorizes the photo proxy and the save-back calls. */ export const POST = async ( req: NextRequest, { params }: { params: Promise<{ sessionId: string }> }, ) => { const { sessionId } = await params; const auth = await authenticateBridge(req, sessionId, "login"); if (!auth) return errorResponse(401, "invalid or expired bridge token"); const token = signBridgeToken({ sid: auth.session.id, eid: auth.session.eventId, uid: auth.user.id, email: auth.user.email, name: auth.user.name, kind: "service", exp: Math.floor(Date.now() / 1000) + BRIDGE_SESSION_TTL_SECONDS, }); return jsonResponse({ token, sessionId: auth.session.id, eventId: auth.session.eventId, expiresAt: auth.session.expiresAt, // Carried from the browser-facing request that opened the editor so the // editor can link back to the address the user actually reached. returnUrl: auth.payload.ret ?? bridgeReturnUrl(auth.session.eventId) ?? null, }); };