el.xwx.moe/lib/api/controllers/users/postUser.ts

92 lines
2.6 KiB
TypeScript
Raw Normal View History

2023-02-18 21:32:02 -06:00
import { prisma } from "@/lib/api/db";
2023-02-06 11:59:23 -06:00
import type { NextApiRequest, NextApiResponse } from "next";
import bcrypt from "bcrypt";
const emailEnabled =
2023-07-12 13:26:34 -05:00
process.env.EMAIL_FROM && process.env.EMAIL_SERVER ? true : false;
2023-02-06 11:59:23 -06:00
interface Data {
response: string | object;
2023-02-06 11:59:23 -06:00
}
interface User {
name: string;
2023-07-19 15:39:59 -05:00
username?: string;
2023-07-12 13:26:34 -05:00
email?: string;
2023-02-06 11:59:23 -06:00
password: string;
}
export default async function postUser(
2023-02-06 11:59:23 -06:00
req: NextApiRequest,
res: NextApiResponse<Data>
) {
if (process.env.NEXT_PUBLIC_DISABLE_REGISTRATION === "true") {
return res.status(400).json({ response: "Registration is disabled." });
}
2023-02-06 11:59:23 -06:00
const body: User = req.body;
const checkHasEmptyFields = emailEnabled
2023-07-19 15:39:59 -05:00
? !body.password || !body.name || !body.email
2023-07-12 13:26:34 -05:00
: !body.username || !body.password || !body.name;
2023-11-19 21:28:02 -06:00
if (body.password.length < 8)
return res
.status(400)
.json({ response: "Password must be at least 8 characters." });
2023-07-12 13:26:34 -05:00
if (checkHasEmptyFields)
return res
.status(400)
.json({ response: "Please fill out all the fields." });
// Check email (if enabled)
const checkEmail =
/^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
if (emailEnabled && !checkEmail.test(body.email?.toLowerCase() || ""))
return res.status(400).json({
response: "Please enter a valid email.",
});
2023-07-19 00:23:53 -05:00
// Check username (if email was disabled)
const checkUsername = RegExp("^[a-z0-9_-]{3,31}$");
2023-07-19 17:38:36 -05:00
if (!emailEnabled && !checkUsername.test(body.username?.toLowerCase() || ""))
2023-07-19 00:23:53 -05:00
return res.status(400).json({
response:
"Username has to be between 3-30 characters, no spaces and special characters are allowed.",
});
2023-07-12 13:26:34 -05:00
const checkIfUserExists = await prisma.user.findFirst({
where: emailEnabled
2023-07-12 13:26:34 -05:00
? {
2023-10-23 00:45:31 -05:00
email: body.email?.toLowerCase().trim(),
2023-07-12 13:26:34 -05:00
}
: {
2023-10-23 00:45:31 -05:00
username: (body.username as string).toLowerCase().trim(),
2023-07-12 13:26:34 -05:00
},
});
2023-02-06 11:59:23 -06:00
if (!checkIfUserExists) {
const saltRounds = 10;
const hashedPassword = bcrypt.hashSync(body.password, saltRounds);
await prisma.user.create({
data: {
name: body.name,
2023-07-19 15:39:59 -05:00
username: emailEnabled
? undefined
2023-10-23 00:45:31 -05:00
: (body.username as string).toLowerCase().trim(),
email: emailEnabled ? body.email?.toLowerCase().trim() : undefined,
2023-02-06 11:59:23 -06:00
password: hashedPassword,
},
});
2023-07-19 00:23:53 -05:00
return res.status(201).json({ response: "User successfully created." });
} else if (checkIfUserExists) {
return res.status(400).json({
response: `${emailEnabled ? "Email" : "Username"} already exists.`,
});
2023-02-06 11:59:23 -06:00
}
}