-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5b273b1
commit eb6fe95
Showing
1 changed file
with
76 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,92 @@ | ||
import { Webhook } from 'svix' | ||
import { headers } from 'next/headers' | ||
import { WebhookEvent } from '@clerk/nextjs/server' | ||
import { db } from '@/lib/db' | ||
import { Webhook } from "svix"; | ||
import { headers } from "next/headers"; | ||
import { WebhookEvent } from "@clerk/nextjs/server"; | ||
import { db } from "@/lib/db"; | ||
|
||
export async function POST(req: Request) { | ||
// You can find this in the Clerk Dashboard -> Webhooks -> choose the webhook | ||
const WEBHOOK_SECRET = process.env.CLERK_WEBHOOK_SECRET; | ||
|
||
// You can find this in the Clerk Dashboard -> Webhooks -> choose the webhook | ||
const WEBHOOK_SECRET = process.env.CLERK_WEBHOOK_SECRET | ||
if (!WEBHOOK_SECRET) { | ||
throw new Error( | ||
"Please add CLERK_WEBHOOK_SECRET from Clerk Dashboard to .env or .env.local" | ||
); | ||
} | ||
|
||
if (!WEBHOOK_SECRET) { | ||
throw new Error('Please add CLERK_WEBHOOK_SECRET from Clerk Dashboard to .env or .env.local') | ||
} | ||
// Get the headers | ||
const headerPayload = headers(); | ||
const svix_id = headerPayload.get("svix-id"); | ||
const svix_timestamp = headerPayload.get("svix-timestamp"); | ||
const svix_signature = headerPayload.get("svix-signature"); | ||
|
||
// Get the headers | ||
const headerPayload = headers(); | ||
const svix_id = headerPayload.get("svix-id"); | ||
const svix_timestamp = headerPayload.get("svix-timestamp"); | ||
const svix_signature = headerPayload.get("svix-signature"); | ||
// If there are no headers, error out | ||
if (!svix_id || !svix_timestamp || !svix_signature) { | ||
return new Response("Error occured -- no svix headers", { | ||
status: 400, | ||
}); | ||
} | ||
|
||
// If there are no headers, error out | ||
if (!svix_id || !svix_timestamp || !svix_signature) { | ||
return new Response('Error occured -- no svix headers', { | ||
status: 400 | ||
}) | ||
} | ||
// Get the body | ||
const payload = await req.json(); | ||
const body = JSON.stringify(payload); | ||
|
||
// Get the body | ||
const payload = await req.json() | ||
const body = JSON.stringify(payload); | ||
// Create a new Svix instance with your secret. | ||
const wh = new Webhook(WEBHOOK_SECRET); | ||
|
||
// Create a new Svix instance with your secret. | ||
const wh = new Webhook(WEBHOOK_SECRET); | ||
let evt: WebhookEvent; | ||
|
||
let evt: WebhookEvent | ||
// Verify the payload with the headers | ||
try { | ||
evt = wh.verify(body, { | ||
"svix-id": svix_id, | ||
"svix-timestamp": svix_timestamp, | ||
"svix-signature": svix_signature, | ||
}) as WebhookEvent; | ||
} catch (err) { | ||
console.error("Error verifying webhook:", err); | ||
return new Response("Error occured", { | ||
status: 400, | ||
}); | ||
} | ||
|
||
// Verify the payload with the headers | ||
try { | ||
evt = wh.verify(body, { | ||
"svix-id": svix_id, | ||
"svix-timestamp": svix_timestamp, | ||
"svix-signature": svix_signature, | ||
}) as WebhookEvent | ||
} catch (err) { | ||
console.error('Error verifying webhook:', err); | ||
return new Response('Error occured', { | ||
status: 400 | ||
}) | ||
} | ||
const eventType = evt.type; | ||
|
||
const eventType = evt.type; | ||
if (eventType === "user.created") { | ||
await db.user.create({ | ||
data: { | ||
externalUserId: payload.data.id, | ||
username: payload.data.username, | ||
imageUrl: payload.data.image_url, | ||
//stream: { | ||
// create: { | ||
// name: `${payload.data.username}'s stream`, | ||
// }, | ||
//}, | ||
}, | ||
}); | ||
} | ||
|
||
if (eventType === 'user.created') { | ||
await db.user.create({ | ||
data: { | ||
externalUserId: payload.data.id, | ||
username: payload.data.username, | ||
imageUrl: payload.data.image_url, | ||
//stream: { | ||
// create: { | ||
// name: `${payload.data.username}'s stream`, | ||
// }, | ||
//}, | ||
}, | ||
}) | ||
} | ||
if (eventType === "user.updated") { | ||
await db.user.update({ | ||
where: { | ||
externalUserId: payload.data.id, | ||
}, | ||
data: { | ||
username: payload.data.username, | ||
imageUrl: payload.data.image_url, | ||
}, | ||
}); | ||
} | ||
|
||
if (eventType === "user.updated") { | ||
await db.user.update({ | ||
where: { | ||
externalUserId: payload.data.id, | ||
}, | ||
data: { | ||
username: payload.data.username, | ||
imageUrl: payload.data.image_url, | ||
}, | ||
}); | ||
} | ||
if (eventType === "user.deleted") { | ||
// await resetIngresses(payload.data.id); | ||
|
||
if (eventType === "user.deleted") { | ||
// await resetIngresses(payload.data.id); | ||
await db.user.delete({ | ||
where: { | ||
externalUserId: payload.data.id, | ||
}, | ||
}); | ||
} | ||
|
||
await db.user.delete({ | ||
where: { | ||
externalUserId: payload.data.id, | ||
}, | ||
}); | ||
} | ||
|
||
return new Response('', { status: 200 }) | ||
return new Response("", { status: 200 }); | ||
} |