2023-02-16 22:39:57 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2023-06-07 07:27:48 +00:00
|
|
|
import { z } from "zod";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2023-03-10 23:45:24 +00:00
|
|
|
import { hashPassword } from "@calcom/features/auth/lib/hashPassword";
|
2023-06-07 07:27:48 +00:00
|
|
|
import { sendEmailVerification } from "@calcom/features/auth/lib/verifyEmail";
|
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";
|
2023-05-02 11:44:05 +00:00
|
|
|
import { IdentityProvider } from "@calcom/prisma/enums";
|
2022-07-28 19:58:26 +00:00
|
|
|
|
2023-06-07 07:27:48 +00:00
|
|
|
const signupSchema = z.object({
|
|
|
|
username: z.string(),
|
|
|
|
email: z.string().email(),
|
|
|
|
password: z.string().min(7),
|
|
|
|
language: z.string().optional(),
|
|
|
|
});
|
|
|
|
|
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;
|
2023-06-07 07:27:48 +00:00
|
|
|
const { email, password, language } = signupSchema.parse(data);
|
|
|
|
|
2021-09-17 23:08:02 +00:00
|
|
|
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
|
|
|
|
2023-05-03 13:10:28 +00:00
|
|
|
// There is an existingUser if the username matches
|
|
|
|
// OR if the email matches AND either the email is verified
|
|
|
|
// or 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-05-03 13:10:28 +00:00
|
|
|
AND: [
|
|
|
|
{ email: userEmail },
|
|
|
|
{
|
|
|
|
OR: [
|
|
|
|
{ emailVerified: { not: null } },
|
|
|
|
{
|
|
|
|
AND: [{ password: { not: null } }, { username: { not: null } }],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
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
|
|
|
}
|
|
|
|
|
2023-06-07 07:27:48 +00:00
|
|
|
await sendEmailVerification({
|
|
|
|
email: userEmail,
|
|
|
|
username,
|
|
|
|
language,
|
|
|
|
});
|
|
|
|
|
2021-08-19 12:27:01 +00:00
|
|
|
res.status(201).json({ message: "Created user" });
|
|
|
|
}
|