@clerk/express
Version:
Clerk server SDK for usage with Express
49 lines (46 loc) • 1.84 kB
JavaScript
import { n as incomingMessageToRequest } from "./utils-DtX1zZA7.mjs";
import { verifyWebhook as verifyWebhook$1 } from "@clerk/backend/webhooks";
export * from "@clerk/backend/webhooks"
//#region src/webhooks.ts
/**
* Verifies the authenticity of a webhook request using Svix.
*
* @param request - The incoming webhook Express Request object
* @param options - Optional configuration object
* @param options.signingSecret - Custom signing secret. If not provided, falls back to CLERK_WEBHOOK_SIGNING_SECRET env variable
* @throws Will throw an error if the webhook signature verification fails
* @returns A promise that resolves to the verified webhook event data
*
* @example
* ```typescript
* import { verifyWebhook } from '@clerk/express/webhooks';
*
* app.post('/api/webhooks', async (req, res) => {
* try {
* const evt = await verifyWebhook(req);
* // handle event
* res.send('Webhook received');
* } catch (err) {
* res.status(400).send('Webhook verification failed');
* }
* });
* ```
*
* @see {@link https://clerk.com/docs/webhooks/sync-data} to learn more about syncing Clerk data to your application using webhooks
*/
async function verifyWebhook(req, options) {
const webRequest = incomingMessageToRequest(req);
let serializedBody;
if (typeof req.body === "string") serializedBody = req.body;
else if (Buffer.isBuffer(req.body)) serializedBody = req.body.toString("utf8");
else if (req.body === void 0 || req.body === null) serializedBody = "";
else try {
serializedBody = JSON.stringify(req.body);
} catch (error) {
throw new Error(`Failed to serialize request body: ${error instanceof Error ? error.message : "Unknown error"}`);
}
return verifyWebhook$1(new Request(webRequest, { body: serializedBody }), options);
}
//#endregion
export { verifyWebhook };
//# sourceMappingURL=webhooks.mjs.map