added username check

This commit is contained in:
Daniel
2023-07-19 01:23:53 -04:00
parent ac2fab1476
commit 742e17351e
2 changed files with 21 additions and 2 deletions
+9
View File
@@ -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 },
+12 -2
View File
@@ -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." });
}
}