pollo/app/api/webhooks/clerk/route.ts

87 lines
2.3 KiB
TypeScript
Raw Normal View History

2023-08-20 17:20:24 -06:00
import { type NextRequest, NextResponse } from "next/server";
2023-08-14 21:16:11 -06:00
import {
onUserCreatedHandler,
onUserDeletedHandler,
2023-09-24 23:49:24 -06:00
} from "app/_utils/webhookHelpers";
2023-09-09 19:25:23 -06:00
import { headers } from "next/headers";
import type { WebhookEvent } from "@clerk/nextjs/server";
import { Webhook } from "svix";
2023-09-24 23:49:24 -06:00
import { env } from "env.mjs";
2023-08-14 21:16:11 -06:00
export const runtime = "edge";
2023-08-20 17:20:24 -06:00
async function handler(req: NextRequest) {
2023-09-09 19:25:23 -06:00
// Get the headers
const headerPayload = headers();
const svix_id = headerPayload.get("svix-id");
const svix_timestamp = headerPayload.get("svix-timestamp");
const svix_signature = headerPayload.get("svix-signature");
// If there are no headers, error out
if (!svix_id || !svix_timestamp || !svix_signature) {
return new Response("Error occured -- no svix headers", {
status: 400,
});
}
// Get the body
const body = JSON.stringify(await req.json());
// Create a new SVIX instance with your secret.
const wh = new Webhook(env.CLERK_WEBHOOK_SIGNING_SECRET);
let evt: WebhookEvent;
// Verify the payload with the headers
2023-08-15 00:54:46 -06:00
try {
2023-09-09 19:25:23 -06:00
evt = wh.verify(body, {
"svix-id": svix_id,
"svix-timestamp": svix_timestamp,
"svix-signature": svix_signature,
}) as WebhookEvent;
} catch (err) {
console.error("Error verifying webhook:", err);
return new Response("Error occured", {
status: 400,
});
}
2023-08-20 17:20:24 -06:00
2023-09-09 19:25:23 -06:00
// Get the ID and type
const { id } = evt.data;
const eventType = evt.type;
let success = false;
switch (eventType) {
case "user.created":
success = await onUserCreatedHandler(id);
if (success) {
2023-08-20 17:20:24 -06:00
return NextResponse.json(
2023-09-09 19:25:23 -06:00
{ result: "USER CREATED" },
{ status: 200, statusText: "USER CREATED" }
2023-08-20 17:20:24 -06:00
);
2023-09-09 19:25:23 -06:00
} else {
2023-08-20 17:20:24 -06:00
return NextResponse.json(
2023-09-09 19:25:23 -06:00
{ result: "USER WITH THIS ID NOT FOUND" },
{ status: 404, statusText: "USER WITH THIS ID NOT FOUND" }
2023-08-20 17:20:24 -06:00
);
2023-09-09 19:25:23 -06:00
}
case "user.deleted":
success = await onUserDeletedHandler(id);
return NextResponse.json(
{ result: "USER DELETED" },
{ status: 200, statusText: "USER DELETED" }
);
default:
return NextResponse.json(
{ result: "INVALID WEBHOOK EVENT TYPE" },
{ status: 400, statusText: "INVALID WEBHOOK EVENT TYPE" }
);
2023-08-14 21:16:11 -06:00
}
}
export { handler as POST };