-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
filter based on if user is part of org or not
- Loading branch information
1 parent
f4896e5
commit 6b68d0e
Showing
11 changed files
with
1,005 additions
and
157 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,18 @@ | ||
'use node' | ||
|
||
import type { WebhookEvent } from '@clerk/clerk-sdk-node' | ||
import { v } from 'convex/values' | ||
import { Webhook } from 'svix' | ||
|
||
import { internalAction } from './_generated/server' | ||
|
||
const webhookSecret = process.env.CLERK_WEBHOOK_SECRET || `` | ||
|
||
export const fulfill = internalAction({ | ||
args: { headers: v.any(), payload: v.string() }, | ||
handler: async (ctx, args) => { | ||
const wh = new Webhook(webhookSecret) | ||
const payload = wh.verify(args.payload, args.headers) as WebhookEvent | ||
return payload | ||
}, | ||
}) |
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,48 @@ | ||
import { httpRouter } from 'convex/server' | ||
import { internal } from './_generated/api' | ||
import { httpAction } from './_generated/server' | ||
|
||
const http = httpRouter() | ||
|
||
http.route({ | ||
path: '/clerk', | ||
method: 'POST', | ||
handler: httpAction(async (ctx, request) => { | ||
const payloadString = await request.text() | ||
const headerPayload = request.headers | ||
try { | ||
const result = await ctx.runAction(internal.clerk.fulfill, { | ||
payload: payloadString, | ||
headers: { | ||
'svix-id': headerPayload.get('svix-id')!, | ||
'svix-timestamp': headerPayload.get('svix-timestamp')!, | ||
'svix-signature': headerPayload.get('svix-signature')!, | ||
}, | ||
}) | ||
|
||
switch (result.type) { | ||
case 'user.created': | ||
await ctx.runMutation(internal.users.createUser, { | ||
tokenIdentifier: `${process.env.CLERK_HOSTNAME}|${result.data.id}`, | ||
}) | ||
break | ||
case 'organizationMembership.created': | ||
await ctx.runMutation(internal.users.addOrgIdToUser, { | ||
tokenIdentifier: `${process.env.CLERK_HOSTNAME}|${result.data.public_user_data.user_id}`, | ||
orgId: result.data.organization.id, | ||
}) | ||
break | ||
} | ||
|
||
return new Response(null, { | ||
status: 200, | ||
}) | ||
} catch (err) { | ||
return new Response('Webhook Error', { | ||
status: 400, | ||
}) | ||
} | ||
}), | ||
}) | ||
|
||
export default http |
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,43 @@ | ||
import { ConvexError, v } from 'convex/values' | ||
import { MutationCtx, QueryCtx, internalMutation } from './_generated/server' | ||
|
||
export async function getUser( | ||
ctx: QueryCtx | MutationCtx, | ||
tokenIdentifier: string, | ||
) { | ||
const user = await ctx.db | ||
.query('users') | ||
.withIndex('by_tokenIdentifier', (q) => | ||
q.eq('tokenIdentifier', tokenIdentifier), | ||
) | ||
.first() | ||
|
||
if (!user) { | ||
throw new ConvexError('expected user to be defined') | ||
} | ||
|
||
return user | ||
} | ||
|
||
export const createUser = internalMutation({ | ||
args: { tokenIdentifier: v.string() }, | ||
async handler(ctx, args) { | ||
await ctx.db.insert('users', { | ||
tokenIdentifier: args.tokenIdentifier, | ||
orgIds: [], | ||
}) | ||
}, | ||
}) | ||
|
||
export const addOrgIdToUser = internalMutation({ | ||
args: { tokenIdentifier: v.string(), orgId: v.string() }, | ||
async handler(ctx, args) { | ||
const user = await getUser(ctx, args.tokenIdentifier) | ||
|
||
console.log(user) | ||
|
||
await ctx.db.patch(user._id, { | ||
orgIds: [...user.orgIds, args.orgId], | ||
}) | ||
}, | ||
}) |
Oops, something went wrong.