-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
40 lines (35 loc) · 1.09 KB
/
route.ts
File metadata and controls
40 lines (35 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { type NextRequest, NextResponse } from "next/server";
import { authenticateRequest } from "@/lib/auth";
import { getNango } from "@/lib/nango";
/**
* POST /api/nango/connect-session
* Create a Nango Connect session for OAuth setup.
* Body: { integration_id: string }
* Returns: { token: string }
*/
export async function POST(req: NextRequest) {
const auth = await authenticateRequest(req);
if (auth instanceof NextResponse) return auth;
const body = (await req.json()) as { integration_id?: string };
if (!body.integration_id) {
return NextResponse.json(
{ error: "integration_id is required" },
{ status: 400 },
);
}
try {
const nango = getNango();
const session = await nango.createConnectSession({
end_user: { id: auth.userId },
allowed_integrations: [body.integration_id],
});
return NextResponse.json({
data: { token: session.data.token },
});
} catch (err) {
return NextResponse.json(
{ error: err instanceof Error ? err.message : "Failed to create connect session" },
{ status: 500 },
);
}
}