2022-03-22 15:22:20 +00:00
|
|
|
import { IdentityProvider } from "@prisma/client";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2023-01-13 18:58:41 +00:00
|
|
|
import { hashPassword } from "@calcom/lib/auth";
|
2023-01-26 22:51:03 +00:00
|
|
|
import slugify from "@calcom/lib/slugify";
|
2022-08-26 21:10:12 +00:00
|
|
|
import { closeComUpsertTeamUser } from "@calcom/lib/sync/SyncServiceManager";
|
2022-07-28 19:58:26 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
|
2021-09-17 23:08:02 +00:00
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
2021-08-19 12:27:01 +00:00
|
|
|
if (req.method !== "POST") {
|
|
|
|
return;
|
2021-06-09 21:29:31 +00:00
|
|
|
}
|
2021-03-24 15:03:04 +00:00
|
|
|
|
2023-01-14 00:42:13 +00:00
|
|
|
if (process.env.NEXT_PUBLIC_DISABLE_SIGNUP === "true") {
|
|
|
|
res.status(403).json({ message: "Signup is disabled" });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-09 21:29:31 +00:00
|
|
|
const data = req.body;
|
2021-09-17 23:08:02 +00:00
|
|
|
const { email, password } = data;
|
|
|
|
const username = slugify(data.username);
|
2021-10-25 09:29:54 +00:00
|
|
|
const userEmail = email.toLowerCase();
|
2021-03-24 15:03:04 +00:00
|
|
|
|
2021-06-09 21:29:31 +00:00
|
|
|
if (!username) {
|
2021-08-19 12:27:01 +00:00
|
|
|
res.status(422).json({ message: "Invalid username" });
|
2021-06-09 21:29:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-05-06 19:39:22 +00:00
|
|
|
|
2021-10-25 09:29:54 +00:00
|
|
|
if (!userEmail || !userEmail.includes("@")) {
|
2021-08-19 12:27:01 +00:00
|
|
|
res.status(422).json({ message: "Invalid email" });
|
2021-06-09 21:29:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-05-06 19:39:22 +00:00
|
|
|
|
2021-06-09 21:29:31 +00:00
|
|
|
if (!password || password.trim().length < 7) {
|
2021-08-19 12:27:01 +00:00
|
|
|
res.status(422).json({ message: "Invalid input - password should be at least 7 characters long." });
|
2021-06-09 21:29:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-03-24 15:03:04 +00:00
|
|
|
|
2022-01-26 16:56:22 +00:00
|
|
|
// There is actually an existingUser if username matches
|
|
|
|
// OR if email matches and both username and password are set
|
2021-06-09 21:29:31 +00:00
|
|
|
const existingUser = await prisma.user.findFirst({
|
|
|
|
where: {
|
|
|
|
OR: [
|
2022-01-26 16:56:22 +00:00
|
|
|
{ username },
|
2021-06-09 21:29:31 +00:00
|
|
|
{
|
2023-02-08 18:39:56 +00:00
|
|
|
AND: [{ email: userEmail }],
|
2021-11-11 05:44:53 +00:00
|
|
|
},
|
|
|
|
],
|
2021-08-19 12:27:01 +00:00
|
|
|
},
|
2021-06-09 21:29:31 +00:00
|
|
|
});
|
2021-03-24 15:03:04 +00:00
|
|
|
|
2021-06-09 21:29:31 +00:00
|
|
|
if (existingUser) {
|
2021-08-19 12:27:01 +00:00
|
|
|
const message: string =
|
2021-10-25 09:29:54 +00:00
|
|
|
existingUser.email !== userEmail ? "Username already taken" : "Email address is already registered";
|
2021-03-24 15:03:04 +00:00
|
|
|
|
2021-08-19 12:27:01 +00:00
|
|
|
return res.status(409).json({ message });
|
2021-06-09 21:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const hashedPassword = await hashPassword(password);
|
|
|
|
|
2022-01-29 18:56:40 +00:00
|
|
|
const user = await prisma.user.upsert({
|
2021-10-25 09:29:54 +00:00
|
|
|
where: { email: userEmail },
|
2021-06-09 21:29:31 +00:00
|
|
|
update: {
|
|
|
|
username,
|
|
|
|
password: hashedPassword,
|
|
|
|
emailVerified: new Date(Date.now()),
|
2022-01-13 20:05:23 +00:00
|
|
|
identityProvider: IdentityProvider.CAL,
|
2021-06-09 21:29:31 +00:00
|
|
|
},
|
|
|
|
create: {
|
|
|
|
username,
|
2021-10-25 09:29:54 +00:00
|
|
|
email: userEmail,
|
2021-06-09 21:29:31 +00:00
|
|
|
password: hashedPassword,
|
2022-01-13 20:05:23 +00:00
|
|
|
identityProvider: IdentityProvider.CAL,
|
2021-08-19 12:27:01 +00:00
|
|
|
},
|
2021-06-09 21:29:31 +00:00
|
|
|
});
|
2021-03-24 15:03:04 +00:00
|
|
|
|
2022-01-29 18:56:40 +00:00
|
|
|
// If user has been invitedTo a team, we accept the membership
|
|
|
|
if (user.invitedTo) {
|
2022-08-26 21:10:12 +00:00
|
|
|
const team = await prisma.team.findFirst({
|
|
|
|
where: { id: user.invitedTo },
|
2022-01-29 18:56:40 +00:00
|
|
|
});
|
2022-08-26 21:10:12 +00:00
|
|
|
|
|
|
|
if (team) {
|
|
|
|
const membership = await prisma.membership.update({
|
|
|
|
where: {
|
|
|
|
userId_teamId: { userId: user.id, teamId: user.invitedTo },
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
accepted: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// Sync Services: Close.com
|
|
|
|
closeComUpsertTeamUser(team, user, membership.role);
|
|
|
|
}
|
2022-01-29 18:56:40 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 12:27:01 +00:00
|
|
|
res.status(201).json({ message: "Created user" });
|
|
|
|
}
|