my-fullstack-ai-platform/app/api/linkedin/callback/route.ts

49 lines
1.3 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { createClient } from "@supabase/supabase-js";
// This endpoint is called by Unipile (unauthenticated), so we use the service-role client.
export async function POST(request: NextRequest) {
const body = await request.json().catch(() => null);
if (!body) {
return NextResponse.json({ error: "Invalid JSON body" }, { status: 400 });
}
// Unipile sends: { account_id, name, status, ... }
const { account_id, name: userId, status } = body as {
account_id?: string;
name?: string;
status?: string;
};
if (!account_id || !userId) {
return NextResponse.json(
{ error: "Missing account_id or user identifier" },
{ status: 400 }
);
}
const supabase = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.SUPABASE_SERVICE_ROLE_KEY!
);
const { data, error } = await supabase
.from("profiles")
.update({
unipile_account_id: account_id,
unipile_account_status: status ?? "CONNECTED",
})
.eq("id", userId)
.select();
if (error) {
return NextResponse.json({ error: "DB update failed" }, { status: 500 });
}
if (!data || data.length === 0) {
return NextResponse.json({ error: "Profile not found" }, { status: 404 });
}
return NextResponse.json({ ok: true });
}