Replies: 2 comments
-
clerk has webhook on user create/update/delete events, |
Beta Was this translation helpful? Give feedback.
0 replies
-
Hey! 👋 I had a similar setup with Clerk + Prisma and solved it this way 👇 ✅ Recommended Approach: Call an API to upsert user on login
import { useUser } from "@clerk/nextjs";
useEffect(() => {
if (user) {
fetch("/api/create-user", {
method: "POST",
body: JSON.stringify({
id: user.id,
email: user.emailAddresses[0].emailAddress,
name: user.fullName,
}),
headers: {
"Content-Type": "application/json",
},
});
}
}, [user]); and in /pages/api/create-user.ts: import { prisma } from "@/lib/prisma"; // or your prisma client
import type { NextApiRequest, NextApiResponse } from "next";
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const { id, email, name } = req.body;
try {
const user = await prisma.user.upsert({
where: { id },
update: {},
create: {
id,
email,
name,
},
});
res.status(200).json(user);
} catch (err) {
res.status(500).json({ error: "Error creating user" });
}
} i hope this example will help me |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
I'm using Clerk for auth and Prisma for db management and I have a dashboard page set up with clerk protection and I want whenever a user logs in or create an account there should be a function to check if the user already exist or not. If no then create a user in my own db. how can I pull this off?
for reference this is how my prisma db of users looks like:
model User {
id String @id
email String @unique
name String?
credits Int @default(10)
createdAt DateTime @default(now())
links Link[]
payments Payment[]
visits Visit[]
}
Additional information
No response
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions