Skip to content

Instantly share code, notes, and snippets.

@bmorrisondev
Created May 27, 2025 20:12
Show Gist options
  • Select an option

  • Save bmorrisondev/926d9321b701f13f529db111953ce931 to your computer and use it in GitHub Desktop.

Select an option

Save bmorrisondev/926d9321b701f13f529db111953ce931 to your computer and use it in GitHub Desktop.
A Supabase Edge Function to handle Clerk Webhooks
import { createClient } from 'npm:@supabase/supabase-js'
import { verifyWebhook } from 'npm:@clerk/backend/webhooks'
Deno.serve(async (req) => {
// Verify webhook signature
const webhookSecret = Deno.env.get('CLERK_WEBHOOK_SECRET')
if (!webhookSecret) {
return new Response('Webhook secret not configured', { status: 500 })
}
const event = await verifyWebhook(req, { signingSecret: webhookSecret });
// Create supabase client
const supabaseUrl = Deno.env.get('SUPABASE_URL')
const supabaseServiceKey = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')
if (!supabaseUrl || !supabaseServiceKey) {
return new Response('Supabase credentials not configured', { status: 500 })
}
const supabase = createClient(supabaseUrl, supabaseServiceKey)
switch (event.type) {
case 'user.created': {
// Handle user creation
const { data: user, error } = await supabase
.from('users')
.insert([
{
id: event.data.id,
first_name: event.data.first_name,
last_name: event.data.last_name,
avatar_url: event.data.image_url,
created_at: new Date(event.data.created_at).toISOString(),
updated_at: new Date(event.data.updated_at).toISOString(),
},
])
.select()
.single()
if (error) {
console.error('Error creating user:', error)
return new Response(JSON.stringify({ error: error.message }), { status: 500 })
}
return new Response(JSON.stringify({ user }), { status: 200 })
}
default: {
// Unhandled event type
console.log('Unhandled event type:', JSON.stringify(event, null, 2))
return new Response(JSON.stringify({ success: true }), { status: 200 })
}
}
})
@bmorrisondev
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment