|
| 1 | +import { Webhook } from 'svix' |
| 2 | +import { StarbaseApp, StarbaseContext } from '../../src/handler' |
| 3 | +import { StarbasePlugin } from '../../src/plugin' |
| 4 | +import { createResponse } from '../../src/utils' |
| 5 | +import CREATE_TABLE from './sql/create-table.sql' |
| 6 | +import UPSERT_USER from './sql/upsert-user.sql' |
| 7 | +import GET_USER_INFORMATION from './sql/get-user-information.sql' |
| 8 | +import DELETE_USER from './sql/delete-user.sql' |
| 9 | + |
| 10 | +type ClerkEvent = { |
| 11 | + instance_id: string |
| 12 | +} & ( |
| 13 | + | { |
| 14 | + type: 'user.created' | 'user.updated' |
| 15 | + data: { |
| 16 | + id: string |
| 17 | + first_name: string |
| 18 | + last_name: string |
| 19 | + email_addresses: Array<{ |
| 20 | + id: string |
| 21 | + email_address: string |
| 22 | + }> |
| 23 | + primary_email_address_id: string |
| 24 | + } |
| 25 | + } |
| 26 | + | { |
| 27 | + type: 'user.deleted' |
| 28 | + data: { id: string } |
| 29 | + } |
| 30 | +) |
| 31 | + |
| 32 | +const SQL_QUERIES = { |
| 33 | + CREATE_TABLE, |
| 34 | + UPSERT_USER, |
| 35 | + GET_USER_INFORMATION, // Currently not used, but can be turned into an endpoint |
| 36 | + DELETE_USER, |
| 37 | +} |
| 38 | + |
| 39 | +export class ClerkPlugin extends StarbasePlugin { |
| 40 | + context?: StarbaseContext |
| 41 | + pathPrefix: string = '/clerk' |
| 42 | + clerkInstanceId?: string |
| 43 | + clerkSigningSecret: string |
| 44 | + |
| 45 | + constructor(opts?: { |
| 46 | + clerkInstanceId?: string |
| 47 | + clerkSigningSecret: string |
| 48 | + }) { |
| 49 | + super('starbasedb:clerk', { |
| 50 | + // The `requiresAuth` is set to false to allow for the webhooks sent by Clerk to be accessible |
| 51 | + requiresAuth: false, |
| 52 | + }) |
| 53 | + if (!opts?.clerkSigningSecret) { |
| 54 | + throw new Error('A signing secret is required for this plugin.') |
| 55 | + } |
| 56 | + this.clerkInstanceId = opts.clerkInstanceId |
| 57 | + this.clerkSigningSecret = opts.clerkSigningSecret |
| 58 | + } |
| 59 | + |
| 60 | + override async register(app: StarbaseApp) { |
| 61 | + app.use(async (c, next) => { |
| 62 | + this.context = c |
| 63 | + const dataSource = c?.get('dataSource') |
| 64 | + |
| 65 | + // Create user table if it doesn't exist |
| 66 | + await dataSource?.rpc.executeQuery({ |
| 67 | + sql: SQL_QUERIES.CREATE_TABLE, |
| 68 | + params: [], |
| 69 | + }) |
| 70 | + |
| 71 | + await next() |
| 72 | + }) |
| 73 | + |
| 74 | + // Webhook to handle Clerk events |
| 75 | + app.post(`${this.pathPrefix}/webhook`, async (c) => { |
| 76 | + const wh = new Webhook(this.clerkSigningSecret) |
| 77 | + const svix_id = c.req.header('svix-id') |
| 78 | + const svix_signature = c.req.header('svix-signature') |
| 79 | + const svix_timestamp = c.req.header('svix-timestamp') |
| 80 | + |
| 81 | + if (!svix_id || !svix_signature || !svix_timestamp) { |
| 82 | + return createResponse( |
| 83 | + undefined, |
| 84 | + 'Missing required headers: svix-id, svix-signature, svix-timestamp', |
| 85 | + 400 |
| 86 | + ) |
| 87 | + } |
| 88 | + |
| 89 | + const body = await c.req.text() |
| 90 | + const dataSource = this.context?.get('dataSource') |
| 91 | + |
| 92 | + try { |
| 93 | + const event = wh.verify(body, { |
| 94 | + 'svix-id': svix_id, |
| 95 | + 'svix-timestamp': svix_timestamp, |
| 96 | + 'svix-signature': svix_signature, |
| 97 | + }) as ClerkEvent |
| 98 | + |
| 99 | + if (this.clerkInstanceId && 'instance_id' in event && event.instance_id !== this.clerkInstanceId) { |
| 100 | + return createResponse( |
| 101 | + undefined, |
| 102 | + 'Invalid instance ID', |
| 103 | + 401 |
| 104 | + ) |
| 105 | + } |
| 106 | + |
| 107 | + if (event.type === 'user.deleted') { |
| 108 | + const { id } = event.data |
| 109 | + |
| 110 | + await dataSource?.rpc.executeQuery({ |
| 111 | + sql: SQL_QUERIES.DELETE_USER, |
| 112 | + params: [id], |
| 113 | + }) |
| 114 | + } else if ( |
| 115 | + event.type === 'user.updated' || |
| 116 | + event.type === 'user.created' |
| 117 | + ) { |
| 118 | + const { id, first_name, last_name, email_addresses, primary_email_address_id } = event.data |
| 119 | + |
| 120 | + const email = email_addresses.find( |
| 121 | + (email: any) => email.id === primary_email_address_id |
| 122 | + )?.email_address |
| 123 | + |
| 124 | + await dataSource?.rpc.executeQuery({ |
| 125 | + sql: SQL_QUERIES.UPSERT_USER, |
| 126 | + params: [id, email, first_name, last_name], |
| 127 | + }) |
| 128 | + } |
| 129 | + |
| 130 | + return createResponse({ success: true }, undefined, 200) |
| 131 | + } catch (error: any) { |
| 132 | + console.error('Webhook processing error:', error) |
| 133 | + return createResponse( |
| 134 | + undefined, |
| 135 | + `Webhook processing failed: ${error.message}`, |
| 136 | + 400 |
| 137 | + ) |
| 138 | + } |
| 139 | + }) |
| 140 | + } |
| 141 | +} |
0 commit comments