Skip to content

Commit

Permalink
fix(api/member): check user existence before insert to database (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsun969 authored Feb 27, 2024
1 parent 82c07ca commit 1b65404
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/app/api/member/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { db } from '@/db';
import { memberTable } from '@/db/schema';
import { checkUserExists } from '@/server/check-user-exists';
import { currentUser } from '@clerk/nextjs';
import { createInsertSchema } from 'drizzle-zod';
import { z } from 'zod';
Expand All @@ -21,6 +22,12 @@ export async function POST(request: Request) {
return new Response(JSON.stringify(reqBody.error.format()), { status: 400 });
}

// Avoid duplicate users in database
const userExists = await checkUserExists(user.id);
if (userExists) {
return new Response(null, { status: 403 });
}

await db.insert(memberTable).values({
clerkId: user.id,
email: user.emailAddresses[0].emailAddress,
Expand Down

0 comments on commit 1b65404

Please sign in to comment.