|
| 1 | +/* eslint-disable import/export */ |
| 2 | +import type { VerifyWebhookOptions } from '@clerk/backend/webhooks'; |
| 3 | +import { verifyWebhook as verifyWebhookBase } from '@clerk/backend/webhooks'; |
| 4 | + |
| 5 | +import { getHeader, isNextRequest, isRequestWebAPI } from './server/headers-utils'; |
| 6 | +import type { RequestLike } from './server/types'; |
| 7 | +// Ordering of exports matter here since |
| 8 | +// we're overriding the base verifyWebhook |
1 | 9 | export * from '@clerk/backend/webhooks';
|
| 10 | + |
| 11 | +const SVIX_ID_HEADER = 'svix-id'; |
| 12 | +const SVIX_TIMESTAMP_HEADER = 'svix-timestamp'; |
| 13 | +const SVIX_SIGNATURE_HEADER = 'svix-signature'; |
| 14 | + |
| 15 | +/** |
| 16 | + * Verifies the authenticity of a webhook request using Svix. |
| 17 | + * |
| 18 | + * @param request - The incoming webhook request object |
| 19 | + * @param options - Optional configuration object |
| 20 | + * @param options.signingSecret - Custom signing secret. If not provided, falls back to CLERK_WEBHOOK_SIGNING_SECRET env variable |
| 21 | + * @throws Will throw an error if the webhook signature verification fails |
| 22 | + * @returns A promise that resolves to the verified webhook event data |
| 23 | + * |
| 24 | + * @example |
| 25 | + * ```typescript |
| 26 | + * import { verifyWebhook } from '@clerk/nextjs/webhooks'; |
| 27 | + * |
| 28 | + * export async function POST(req: Request) { |
| 29 | + * try { |
| 30 | + * const evt = await verifyWebhook(req); |
| 31 | + * |
| 32 | + * // Access the event data |
| 33 | + * const { id } = evt.data; |
| 34 | + * const eventType = evt.type; |
| 35 | + * |
| 36 | + * // Handle specific event types |
| 37 | + * if (evt.type === 'user.created') { |
| 38 | + * console.log('New user created:', evt.data.id); |
| 39 | + * // Handle user creation |
| 40 | + * } |
| 41 | + * |
| 42 | + * return new Response('Success', { status: 200 }); |
| 43 | + * } catch (err) { |
| 44 | + * console.error('Webhook verification failed:', err); |
| 45 | + * return new Response('Webhook verification failed', { status: 400 }); |
| 46 | + * } |
| 47 | + * } |
| 48 | + * ``` |
| 49 | + */ |
| 50 | +export async function verifyWebhook(request: RequestLike, options?: VerifyWebhookOptions) { |
| 51 | + if (isNextRequest(request) || isRequestWebAPI(request)) { |
| 52 | + return verifyWebhookBase(request, options); |
| 53 | + } |
| 54 | + |
| 55 | + const webRequest = nextApiRequestToWebRequest(request); |
| 56 | + return verifyWebhookBase(webRequest, options); |
| 57 | +} |
| 58 | + |
| 59 | +function nextApiRequestToWebRequest(req: RequestLike): Request { |
| 60 | + const headers = new Headers(); |
| 61 | + const svixId = getHeader(req, SVIX_ID_HEADER) || ''; |
| 62 | + const svixTimestamp = getHeader(req, SVIX_TIMESTAMP_HEADER) || ''; |
| 63 | + const svixSignature = getHeader(req, SVIX_SIGNATURE_HEADER) || ''; |
| 64 | + |
| 65 | + headers.set(SVIX_ID_HEADER, svixId); |
| 66 | + headers.set(SVIX_TIMESTAMP_HEADER, svixTimestamp); |
| 67 | + headers.set(SVIX_SIGNATURE_HEADER, svixSignature); |
| 68 | + |
| 69 | + // Create a dummy URL to make a Request object |
| 70 | + const protocol = getHeader(req, 'x-forwarded-proto') || 'http'; |
| 71 | + const host = getHeader(req, 'x-forwarded-host') || 'clerk-dummy'; |
| 72 | + const dummyOriginReqUrl = new URL(req.url || '', `${protocol}://${host}`); |
| 73 | + |
| 74 | + const body = 'body' in req && req.body ? JSON.stringify(req.body) : undefined; |
| 75 | + |
| 76 | + return new Request(dummyOriginReqUrl, { |
| 77 | + method: req.method, |
| 78 | + headers, |
| 79 | + body, |
| 80 | + }); |
| 81 | +} |
0 commit comments