@clerk/express
Version:
Clerk server SDK for usage with Express
55 lines (52 loc) • 2.17 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_utils = require('./utils-ChU4G25X.js');
let _clerk_backend_webhooks = require("@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 = require_utils.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 (0, _clerk_backend_webhooks.verifyWebhook)(new Request(webRequest, { body: serializedBody }), options);
}
//#endregion
exports.verifyWebhook = verifyWebhook;
Object.keys(_clerk_backend_webhooks).forEach(function (k) {
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
enumerable: true,
get: function () { return _clerk_backend_webhooks[k]; }
});
});
//# sourceMappingURL=webhooks.js.map