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-07 15:59:00 -05:00
|
|
|
import isServerAdmin from "../../isServerAdmin";
|
2024-09-14 15:00:19 -05:00
|
|
|
import { PostUserSchema } from "@/lib/shared/schemaValidation";
|
2023-02-06 11:59:23 -06:00
|
|
|
|
2023-07-15 21:15:43 -05: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 {
|
2023-06-26 17:33:40 -05:00
|
|
|
response: string | object;
|
2024-05-07 15:59:00 -05:00
|
|
|
status: number;
|
2023-02-06 11:59:23 -06:00
|
|
|
}
|
|
|
|
|
2023-10-22 23:28:39 -05:00
|
|
|
export default async function postUser(
|
2023-02-06 11:59:23 -06:00
|
|
|
req: NextApiRequest,
|
2024-05-07 15:59:00 -05:00
|
|
|
res: NextApiResponse
|
|
|
|
): Promise<Data> {
|
|
|
|
let isAdmin = await isServerAdmin({ req });
|
|
|
|
|
|
|
|
if (process.env.NEXT_PUBLIC_DISABLE_REGISTRATION === "true" && !isAdmin) {
|
|
|
|
return { response: "Registration is disabled.", status: 400 };
|
2023-09-28 10:37:25 -05:00
|
|
|
}
|
|
|
|
|
2024-09-14 15:00:19 -05:00
|
|
|
const dataValidation = PostUserSchema().safeParse(req.body);
|
2023-11-19 21:28:02 -06:00
|
|
|
|
2024-09-14 15:00:19 -05:00
|
|
|
if (!dataValidation.success) {
|
|
|
|
return {
|
|
|
|
response: `Error: ${
|
|
|
|
dataValidation.error.issues[0].message
|
|
|
|
} [${dataValidation.error.issues[0].path.join(", ")}]`,
|
|
|
|
status: 400,
|
|
|
|
};
|
|
|
|
}
|
2023-07-19 00:23:53 -05:00
|
|
|
|
2024-09-14 15:00:19 -05:00
|
|
|
const { name, email, password } = dataValidation.data;
|
|
|
|
let { username } = dataValidation.data;
|
2024-08-14 16:26:38 -05:00
|
|
|
|
|
|
|
const autoGeneratedUsername = "user" + Math.round(Math.random() * 1000000000);
|
|
|
|
|
2024-09-14 15:00:19 -05:00
|
|
|
if (!username) {
|
|
|
|
username = autoGeneratedUsername;
|
2024-08-14 16:26:38 -05:00
|
|
|
}
|
2023-07-19 00:23:53 -05:00
|
|
|
|
2023-07-12 13:26:34 -05:00
|
|
|
const checkIfUserExists = await prisma.user.findFirst({
|
2024-05-03 09:22:45 -05:00
|
|
|
where: {
|
|
|
|
OR: [
|
|
|
|
{
|
2024-09-14 15:00:19 -05:00
|
|
|
email: email ? email.toLowerCase().trim() : undefined,
|
2024-05-03 09:22:45 -05:00
|
|
|
},
|
|
|
|
{
|
2024-09-14 15:00:19 -05:00
|
|
|
username: username ? username.toLowerCase().trim() : undefined,
|
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) {
|
2024-05-12 23:27:29 -05:00
|
|
|
const autoGeneratedUsername =
|
|
|
|
"user" + Math.round(Math.random() * 1000000000);
|
|
|
|
|
2023-02-06 11:59:23 -06:00
|
|
|
const saltRounds = 10;
|
|
|
|
|
2024-09-14 15:00:19 -05:00
|
|
|
const hashedPassword = bcrypt.hashSync(password, saltRounds);
|
2023-02-06 11:59:23 -06:00
|
|
|
|
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...
|
|
|
|
|
2024-05-07 15:59:00 -05:00
|
|
|
if (isAdmin) {
|
2024-05-03 09:22:45 -05:00
|
|
|
const user = await prisma.user.create({
|
|
|
|
data: {
|
2024-09-14 15:00:19 -05:00
|
|
|
name: name,
|
2024-05-12 23:27:29 -05:00
|
|
|
username: emailEnabled
|
2024-09-14 15:00:19 -05:00
|
|
|
? (username as string) || autoGeneratedUsername
|
|
|
|
: (username as string),
|
|
|
|
email: emailEnabled ? email : undefined,
|
2024-05-03 09:22:45 -05:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-05-07 15:59:00 -05:00
|
|
|
return { response: user, status: 201 };
|
2024-05-03 09:22:45 -05:00
|
|
|
} else {
|
|
|
|
await prisma.user.create({
|
|
|
|
data: {
|
2024-09-14 15:00:19 -05:00
|
|
|
name: name,
|
|
|
|
username: emailEnabled ? autoGeneratedUsername : (username as string),
|
|
|
|
email: emailEnabled ? email : undefined,
|
2024-05-03 09:22:45 -05:00
|
|
|
password: hashedPassword,
|
|
|
|
},
|
|
|
|
});
|
2023-02-06 11:59:23 -06:00
|
|
|
|
2024-05-07 15:59:00 -05:00
|
|
|
return { response: "User successfully created.", status: 201 };
|
2024-05-03 09:22:45 -05:00
|
|
|
}
|
2024-05-07 15:59:00 -05:00
|
|
|
} else {
|
|
|
|
return { response: "Email or Username already exists.", status: 400 };
|
2023-02-06 11:59:23 -06:00
|
|
|
}
|
|
|
|
}
|