Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use unique for clerkId to avoid duplicate user in db #113

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions src/app/api/member/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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 @@ -22,12 +21,6 @@ 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
2 changes: 1 addition & 1 deletion src/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const memberTable = sqliteTable('members', {
.$defaultFn(() => nanoid())
.primaryKey(),

clerkId: text('clerk_id').notNull(),
clerkId: text('clerk_id').notNull().unique(),
email: text('email').notNull(),
firstName: text('first_name').notNull(),
lastName: text('last_name').notNull(),
Expand Down
Loading