From 257cdacad47aaec89ea5e4349345d9069fc99b9c Mon Sep 17 00:00:00 2001 From: Huskydog9988 <39809509+Huskydog9988@users.noreply.github.com> Date: Sat, 22 Mar 2025 17:26:12 -0400 Subject: [PATCH] make signup less error prone in db --- server/api/v1/auth/signup/simple.post.ts | 45 +++++++++++++----------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/server/api/v1/auth/signup/simple.post.ts b/server/api/v1/auth/signup/simple.post.ts index f96af7f..0e1c6f1 100644 --- a/server/api/v1/auth/signup/simple.post.ts +++ b/server/api/v1/auth/signup/simple.post.ts @@ -95,28 +95,31 @@ export default defineEventHandler(async (h3) => { {}, [`internal:read`, `${userId}:write`] ); - const user = await prisma.user.create({ - data: { - id: userId, - username, - displayName, - email, - profilePicture: profilePictureId, - admin: invitation.isAdmin, - }, - }); const hash = await createHashArgon2(password); - await prisma.linkedAuthMec.create({ - data: { - mec: AuthMec.Simple, - credentials: {}, - userId: user.id, - password: hash, - }, - }); + const [linkMec] = await prisma.$transaction([ + prisma.linkedAuthMec.create({ + data: { + mec: AuthMec.Simple, + credentials: {}, + password: hash, + user: { + create: { + id: userId, + username, + displayName, + email, + profilePicture: profilePictureId, + admin: invitation.isAdmin, + }, + }, + }, + select: { + user: true, + }, + }), + prisma.invitation.delete({ where: { id: invitationId } }), + ]); - await prisma.invitation.delete({ where: { id: invitationId } }); - - return user; + return linkMec.user; });