-
Notifications
You must be signed in to change notification settings - Fork 6
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
83b5b83
commit 5394ea7
Showing
11 changed files
with
337 additions
and
21 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
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Webhook } from "svix"; | ||
import { headers } from "next/headers"; | ||
import { WebhookEvent } from "@clerk/nextjs/server"; | ||
|
||
export async function POST(req: Request) { | ||
// You can find this in the Clerk Dashboard -> Webhooks -> choose the webhook | ||
const WEBHOOK_SECRET = process.env.WEBHOOK_SECRET; | ||
|
||
if (!WEBHOOK_SECRET) { | ||
throw new Error( | ||
"Please add 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"); | ||
|
||
// 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); | ||
|
||
// Create a new Svix instance with your secret. | ||
const wh = new Webhook(WEBHOOK_SECRET); | ||
|
||
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, | ||
}); | ||
} | ||
|
||
// Get the ID and type | ||
const { id } = evt.data; | ||
const eventType = evt.type; | ||
|
||
console.log(`Webhook with and ID of ${id} and type of ${eventType}`); | ||
console.log("Webhook body:", body); | ||
|
||
if (eventType === "organization.created") { | ||
console.log("Organization created", evt.data); | ||
} | ||
|
||
return new Response("", { status: 200 }); | ||
} |
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
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
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { connectToDb } from "../../utils/mongoose"; | ||
import Community from "../model/community.model"; | ||
import User from "../model/user.model"; | ||
|
||
interface CommunityCardProps { | ||
name: string; | ||
bio: string; | ||
image: string; | ||
onBoarded?: boolean; | ||
userId: string; | ||
} | ||
|
||
export const createCommunity = async ( | ||
id: string, | ||
name: string, | ||
bio: string, | ||
image: string, | ||
onBoarded: string, | ||
createdBy: string | ||
) => { | ||
try { | ||
await connectToDb(); | ||
const user = await User.findOne({ id: createdBy }); | ||
if (!user) return null; | ||
|
||
const newCommunity = await Community.create({ | ||
name, | ||
bio, | ||
image, | ||
onBoarded, | ||
createdBy, | ||
}); | ||
|
||
user.community.push(newCommunity._id); | ||
await user.save(); | ||
|
||
return newCommunity; | ||
} catch (error: any) { | ||
console.log("Failed to create community :", error.message); | ||
} | ||
}; |
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
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import mongoose from "mongoose"; | ||
|
||
const communitySchema = new mongoose.Schema({ | ||
id: { | ||
type: String, | ||
required: true, | ||
}, | ||
name: { type: String, required: true }, | ||
createdBy: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: "User", | ||
}, | ||
bio: String, | ||
image: String, | ||
members: [ | ||
{ | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: "User", | ||
}, | ||
], | ||
createdAt: { type: Date, default: Date.now() }, | ||
onBoarded: { | ||
type: Boolean, | ||
required: true, | ||
default: false, | ||
}, | ||
}); | ||
|
||
const Community = | ||
mongoose.models.Community || mongoose.model("Community", communitySchema); | ||
|
||
export default Community; |
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
Oops, something went wrong.