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 }) {