46 lines
1.3 KiB
TypeScript
46 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) {
|
|
console.error("Unipile callback missing account_id or name:", body);
|
|
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 { error } = await supabase
|
|
.from("profiles")
|
|
.update({
|
|
unipile_account_id: account_id,
|
|
unipile_account_status: status ?? "CONNECTED",
|
|
})
|
|
.eq("id", userId);
|
|
|
|
if (error) {
|
|
console.error("Error saving Unipile account_id:", error);
|
|
return NextResponse.json({ error: "DB update failed" }, { status: 500 });
|
|
}
|
|
|
|
return NextResponse.json({ ok: true });
|
|
}
|