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

147 lines
4.1 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";
2024-05-03 09:22:45 -05:00
import verifyUser from "../../verifyUser";
2023-02-06 11:59:23 -06:00
const emailEnabled =
2023-07-12 13:26:34 -05:00
process.env.EMAIL_FROM && process.env.EMAIL_SERVER ? true : false;
2024-05-03 09:22:45 -05:00
const stripeEnabled = process.env.STRIPE_SECRET_KEY ? true : false;
2023-07-12 13:26:34 -05:00
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>
) {
2024-05-03 09:22:45 -05:00
let isServerAdmin = false;
const user = await verifyUser({ req, res });
if (process.env.ADMINISTRATOR === user?.username) isServerAdmin = true;
if (
process.env.NEXT_PUBLIC_DISABLE_REGISTRATION === "true" &&
!isServerAdmin
) {
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:32:23 -06:00
if (!body.password || body.password.length < 8)
2023-11-19 21:28:02 -06:00
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({
2024-05-03 09:22:45 -05:00
where: {
OR: [
{
2023-10-23 00:45:31 -05:00
email: body.email?.toLowerCase().trim(),
2024-05-03 09:22:45 -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
},
2024-05-03 09:22:45 -05:00
],
},
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);
2024-05-03 09:22:45 -05:00
// Subscription dates
const currentPeriodStart = new Date();
const currentPeriodEnd = new Date();
currentPeriodEnd.setFullYear(currentPeriodEnd.getFullYear() + 1000); // end date is in 1000 years...
if (isServerAdmin) {
const user = await prisma.user.create({
data: {
name: body.name,
username: (body.username as string).toLowerCase().trim(),
email: emailEnabled ? body.email?.toLowerCase().trim() : undefined,
password: hashedPassword,
emailVerified: new Date(),
subscriptions: stripeEnabled
? {
create: {
stripeSubscriptionId:
"fake_sub_" + Math.round(Math.random() * 10000000000000),
active: true,
currentPeriodStart,
currentPeriodEnd,
},
}
: undefined,
},
select: {
id: true,
username: true,
email: true,
emailVerified: true,
subscriptions: {
select: {
active: true,
},
},
createdAt: true,
},
});
return res.status(201).json({ response: user });
} else {
await prisma.user.create({
data: {
name: body.name,
username: emailEnabled
? undefined
: (body.username as string).toLowerCase().trim(),
email: emailEnabled ? body.email?.toLowerCase().trim() : undefined,
password: hashedPassword,
},
});
2023-02-06 11:59:23 -06:00
2024-05-03 09:22:45 -05:00
return res.status(201).json({ response: "User successfully created." });
}
} else if (checkIfUserExists) {
return res.status(400).json({
2024-05-03 09:22:45 -05:00
response: `Email or Username already exists.`,
});
2023-02-06 11:59:23 -06:00
}
}