From 2264abd3842be851bbb034327187056206d550b1 Mon Sep 17 00:00:00 2001 From: daniel31x13 Date: Thu, 18 Jul 2024 20:29:33 -0400 Subject: [PATCH] bug fixed --- pages/api/v1/auth/[...nextauth].ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pages/api/v1/auth/[...nextauth].ts b/pages/api/v1/auth/[...nextauth].ts index c15f8c7..dd38aa7 100644 --- a/pages/api/v1/auth/[...nextauth].ts +++ b/pages/api/v1/auth/[...nextauth].ts @@ -1200,6 +1200,28 @@ export default async function auth(req: NextApiRequest, res: NextApiResponse) { if (trigger === "signIn" || trigger === "signUp") token.id = user?.id as number; + if (trigger === "signUp") { + const checkIfUserExists = await prisma.user.findUnique({ + where: { + id: token.id, + }, + }); + + if (checkIfUserExists && !checkIfUserExists.username) { + const autoGeneratedUsername = + "user" + Math.round(Math.random() * 1000000000); + + await prisma.user.update({ + where: { + id: token.id, + }, + data: { + username: autoGeneratedUsername, + }, + }); + } + } + return token; }, async session({ session, token }) {