many improvements

This commit is contained in:
Daniel
2023-07-19 16:39:59 -04:00
parent 8f6dfdd868
commit 7912815c9e
14 changed files with 176 additions and 109 deletions
+9 -35
View File
@@ -11,7 +11,7 @@ interface Data {
interface User {
name: string;
username: string;
username?: string;
email?: string;
password: string;
}
@@ -23,7 +23,7 @@ export default async function Index(
const body: User = req.body;
const checkHasEmptyFields = emailEnabled
? !body.username || !body.password || !body.name || !body.email
? !body.password || !body.name || !body.email
: !body.username || !body.password || !body.name;
if (checkHasEmptyFields)
@@ -31,30 +31,9 @@ export default async function Index(
.status(400)
.json({ response: "Please fill out all the fields." });
const tenMinutesAgo = new Date(Date.now() - 10 * 60 * 1000);
// Remove user's who aren't verified for more than 10 minutes
if (emailEnabled)
await prisma.user.deleteMany({
where: {
OR: [
{
email: body.email,
},
{
username: body.username,
},
],
createdAt: {
lt: tenMinutesAgo,
},
emailVerified: null,
},
});
const checkUsername = RegExp("^[a-z0-9_-]{3,31}$");
if (!checkUsername.test(body.username))
if (!emailEnabled && !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.",
@@ -63,18 +42,11 @@ export default async function Index(
const checkIfUserExists = await prisma.user.findFirst({
where: emailEnabled
? {
OR: [
{
username: body.username.toLowerCase(),
},
{
email: body.email?.toLowerCase(),
},
],
email: body.email?.toLowerCase(),
emailVerified: { not: null },
}
: {
username: body.username.toLowerCase(),
username: (body.username as string).toLowerCase(),
},
});
@@ -86,8 +58,10 @@ export default async function Index(
await prisma.user.create({
data: {
name: body.name,
username: body.username.toLowerCase(),
email: body.email?.toLowerCase(),
username: emailEnabled
? undefined
: (body.username as string).toLowerCase(),
email: emailEnabled ? body.email?.toLowerCase() : undefined,
password: hashedPassword,
},
});