Refactor webhook signature verification process

The signature verification process for webhooks has been abstracted. Instead of performing the check directly in the handleWebhook method, it has been moved to a separate service class: PostgresDatabaseWebhookVerifierService. This refactor improves modularity and enables extending or replacing the verification logic more comfortably. The WEBHOOK_SENDER_PROVIDER environment variable is used to determine which verifier service to use.
This commit is contained in:
giancarlo
2024-04-18 21:03:36 +08:00
parent 98a4c02020
commit 4914a56460
6 changed files with 70 additions and 22 deletions

View File

@@ -1,15 +1,5 @@
import { z } from 'zod';
import { DatabaseWebhookHandlerService } from '@kit/database-webhooks';
const webhooksSecret = z
.string({
description: `The secret used to verify the webhook signature`,
required_error: `Provide the variable SUPABASE_DB_WEBHOOK_SECRET. This is used to authenticate the webhook event from Supabase.`,
})
.min(1)
.parse(process.env.SUPABASE_DB_WEBHOOK_SECRET);
const service = new DatabaseWebhookHandlerService();
const response = (status: number) => new Response(null, { status });
@@ -23,7 +13,7 @@ const response = (status: number) => new Response(null, { status });
export async function POST(request: Request) {
try {
// handle the webhook event
await service.handleWebhook(request, webhooksSecret);
await service.handleWebhook(request);
return response(200);
} catch {