From 742e17351e464797683151be0f1d6d2110479c20 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 19 Jul 2023 01:23:53 -0400 Subject: [PATCH] added username check --- lib/api/controllers/users/updateUser.ts | 9 +++++++++ pages/api/auth/register.ts | 14 ++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/api/controllers/users/updateUser.ts b/lib/api/controllers/users/updateUser.ts index 064dd84..cdf6260 100644 --- a/lib/api/controllers/users/updateUser.ts +++ b/lib/api/controllers/users/updateUser.ts @@ -20,6 +20,15 @@ export default async function updateUser( status: 400, }; + const checkUsername = RegExp("^[a-z0-9_-]{3,31}$"); + + if (!checkUsername.test(user.username)) + return { + response: + "Username has to be between 3-30 characters, no spaces and special characters are allowed.", + status: 400, + }; + const userIsTaken = await prisma.user.findFirst({ where: { id: { not: sessionUser.id }, diff --git a/pages/api/auth/register.ts b/pages/api/auth/register.ts index 9e91bee..44406ee 100644 --- a/pages/api/auth/register.ts +++ b/pages/api/auth/register.ts @@ -52,6 +52,14 @@ export default async function Index( }, }); + const checkUsername = RegExp("^[a-z0-9_-]{3,31}$"); + + if (!checkUsername.test(body.username)) + return res.status(400).json({ + response: + "Username has to be between 3-30 characters, no spaces and special characters are allowed.", + }); + const checkIfUserExists = await prisma.user.findFirst({ where: emailEnabled ? { @@ -84,8 +92,10 @@ export default async function Index( }, }); - res.status(201).json({ response: "User successfully created." }); + return res.status(201).json({ response: "User successfully created." }); } else if (checkIfUserExists) { - res.status(400).json({ response: "Username and/or Email already exists." }); + return res + .status(400) + .json({ response: "Username and/or Email already exists." }); } }