Syncing data from clerk sign up to database

Is there a way of syncing new user sign ups on a "User" table on a database without using webhooks?
5 Replies
Circus
Circus11mo ago
I asked this in the Clerk channel and they're adamant on using webhooks for this. I found that process to be very draining and incondite and wish this was simpler. (Going to follow this thread to see other answers though)
DiamondDragon
DiamondDragon11mo ago
Yeah it took me much longer to get the we hooks all setup. I'm syncing three tables myself
vibbin
vibbin11mo ago
I've created a function in my utils folder that I call in my /onboarding page (server component)
export async function syncUser() {

const clerkUser = await currentUser();

if (!clerkUser?.id) {
return null;
}

try {
let user = await db.user.findUnique({
where: {
clerkId: clerkUser.id as string,
},
});

if (!user) {
user = await db.user.create({
data: {
clerkId: clerkUser.id,
name: clerkUser.firstName,
email: clerkUser.emailAddresses?.[0]?.emailAddress,
image: clerkUser.imageUrl,
levels: {
create: { level: 1, xp: 0 },
},
},
});
}

return user;
} catch (error) {
console.error('Error syncing user:', error);
return null;
}
}
export async function syncUser() {

const clerkUser = await currentUser();

if (!clerkUser?.id) {
return null;
}

try {
let user = await db.user.findUnique({
where: {
clerkId: clerkUser.id as string,
},
});

if (!user) {
user = await db.user.create({
data: {
clerkId: clerkUser.id,
name: clerkUser.firstName,
email: clerkUser.emailAddresses?.[0]?.emailAddress,
image: clerkUser.imageUrl,
levels: {
create: { level: 1, xp: 0 },
},
},
});
}

return user;
} catch (error) {
console.error('Error syncing user:', error);
return null;
}
}
rafaelsilva81
rafaelsilva81OP11mo ago
i thought about something like that initially, but i figured maybe there was some other way but I think this is what ill end up doing, thanks!
mardo
mardo10mo ago
I call their getUser on the API and then load my database each time they come into my application.
Want results from more Discord servers?
Add your server