* refactor: apply biome formatting to apps/web (batch 1) Formats apps/web non-modules directories: - apps/web/app - apps/web/lib - apps/web/pages - apps/web/styles - apps/web/server - apps/web/test - Root-level .ts and .mjs files Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * refactor: apply biome formatting to apps/web/modules (batch 2) Formats smaller apps/web/modules directories: - onboarding, data-table, users, apps, auth - integration-attribute-sync, shell, availability - feature-flags, team, webhooks, getting-started - feature-opt-in, troubleshooter, schedules, notifications - signup-view.tsx Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * refactor: apply biome formatting to apps/web/modules (batch 3) Formats medium apps/web/modules directories: - bookings - event-types - insights - embed Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * refactor: apply biome formatting to apps/web/modules/ee (batch 4) Formats apps/web/modules/ee directory. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * refactor: apply biome formatting to apps/web (batch 5) Formats remaining apps/web directories: - apps/web/modules/settings - apps/web/modules/booking-audit - apps/web/playwright Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * refactor: apply biome formatting to apps/web/components (batch 6) Formats remaining apps/web/components files. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * refactor: apply biome formatting to new apps/web files from main Formats newly added/modified files from main merge: - WorkflowStepContainer.tsx (resolved merge conflicts) - Newly moved files (blocklist, form-builder, schedules, etc.) - Other files modified in main Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * refactor: apply biome formatting to new apps/web files from main Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
222 lines
7.5 KiB
TypeScript
222 lines
7.5 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
|
|
import { checkPremiumUsername } from "@calcom/ee/common/lib/checkPremiumUsername";
|
|
import { sendEmailVerification } from "@calcom/features/auth/lib/verifyEmail";
|
|
import { createOrUpdateMemberships } from "@calcom/features/auth/signup/utils/createOrUpdateMemberships";
|
|
import { validateAndGetCorrectedUsernameAndEmail } from "@calcom/features/auth/signup/utils/validateUsername";
|
|
import { hashPassword } from "@calcom/lib/auth/hashPassword";
|
|
import { IS_PREMIUM_USERNAME_ENABLED } from "@calcom/lib/constants";
|
|
import logger from "@calcom/lib/logger";
|
|
import { isPrismaError } from "@calcom/lib/server/getServerErrorFromUnknown";
|
|
import { isUsernameReservedDueToMigration } from "@calcom/lib/server/username";
|
|
import slugify from "@calcom/lib/slugify";
|
|
import prisma from "@calcom/prisma";
|
|
import { IdentityProvider } from "@calcom/prisma/enums";
|
|
import { signupSchema } from "@calcom/prisma/zod-utils";
|
|
|
|
import { joinAnyChildTeamOnOrgInvite } from "@calcom/features/auth/signup/utils/organization";
|
|
import { prefillAvatar } from "@calcom/features/auth/signup/utils/prefillAvatar";
|
|
import { SIGNUP_ERROR_CODES } from "@calcom/features/auth/signup/constants";
|
|
import {
|
|
findTokenByToken,
|
|
throwIfTokenExpired,
|
|
validateAndGetCorrectedUsernameForTeam,
|
|
} from "@calcom/features/auth/signup/utils/token";
|
|
|
|
export default async function handler(body: Record<string, string>) {
|
|
const { email, password, language, token } = signupSchema.parse(body);
|
|
|
|
const username = slugify(body.username);
|
|
const userEmail = email.toLowerCase();
|
|
|
|
if (!username) {
|
|
return NextResponse.json({ message: "Invalid username" }, { status: 422 });
|
|
}
|
|
|
|
let foundToken: { id: number; teamId: number | null; expires: Date } | null = null;
|
|
let correctedUsername = username;
|
|
if (token) {
|
|
foundToken = await findTokenByToken({ token });
|
|
throwIfTokenExpired(foundToken?.expires);
|
|
correctedUsername = await validateAndGetCorrectedUsernameForTeam({
|
|
username,
|
|
email: userEmail,
|
|
teamId: foundToken?.teamId,
|
|
isSignup: true,
|
|
});
|
|
|
|
if (foundToken?.teamId) {
|
|
const existingUser = await prisma.user.findUnique({
|
|
where: { email: userEmail },
|
|
select: { invitedTo: true },
|
|
});
|
|
if (existingUser && existingUser.invitedTo !== foundToken.teamId) {
|
|
return NextResponse.json({ message: SIGNUP_ERROR_CODES.USER_ALREADY_EXISTS }, { status: 409 });
|
|
}
|
|
}
|
|
} else {
|
|
const userValidation = await validateAndGetCorrectedUsernameAndEmail({
|
|
username,
|
|
email: userEmail,
|
|
isSignup: true,
|
|
});
|
|
if (!userValidation.isValid) {
|
|
logger.error("User validation failed", { userValidation });
|
|
return NextResponse.json({ message: "Username or email is already taken" }, { status: 409 });
|
|
}
|
|
if (!userValidation.username) {
|
|
return NextResponse.json({ message: "Invalid username" }, { status: 422 });
|
|
}
|
|
correctedUsername = userValidation.username;
|
|
}
|
|
|
|
const hashedPassword = await hashPassword(password);
|
|
|
|
if (foundToken && foundToken?.teamId) {
|
|
const team = await prisma.team.findUnique({
|
|
where: {
|
|
id: foundToken.teamId,
|
|
},
|
|
include: {
|
|
parent: {
|
|
select: {
|
|
id: true,
|
|
slug: true,
|
|
organizationSettings: true,
|
|
},
|
|
},
|
|
organizationSettings: true,
|
|
},
|
|
});
|
|
|
|
if (team) {
|
|
const isInviteForATeamInOrganization = !!team.parent;
|
|
const isCheckingUsernameInGlobalNamespace = !team.isOrganization && !isInviteForATeamInOrganization;
|
|
|
|
if (isCheckingUsernameInGlobalNamespace) {
|
|
const isUsernameAvailable = !(await isUsernameReservedDueToMigration(correctedUsername));
|
|
if (!isUsernameAvailable) {
|
|
return NextResponse.json({ message: "A user exists with that username" }, { status: 409 });
|
|
}
|
|
}
|
|
|
|
const organizationId = team.isOrganization ? team.id : (team.parent?.id ?? null);
|
|
|
|
const existingUserByUsername = await prisma.user.findFirst({
|
|
where: {
|
|
username: correctedUsername,
|
|
organizationId,
|
|
NOT: { email: userEmail },
|
|
},
|
|
select: { id: true },
|
|
});
|
|
if (existingUserByUsername) {
|
|
return NextResponse.json({ message: SIGNUP_ERROR_CODES.USER_ALREADY_EXISTS }, { status: 409 });
|
|
}
|
|
|
|
let user: { id: number };
|
|
try {
|
|
user = await prisma.user.upsert({
|
|
where: { email: userEmail },
|
|
update: {
|
|
username: correctedUsername,
|
|
emailVerified: new Date(Date.now()),
|
|
identityProvider: IdentityProvider.CAL,
|
|
password: {
|
|
upsert: {
|
|
create: { hash: hashedPassword },
|
|
update: { hash: hashedPassword },
|
|
},
|
|
},
|
|
organizationId,
|
|
},
|
|
create: {
|
|
username: correctedUsername,
|
|
email: userEmail,
|
|
emailVerified: new Date(Date.now()),
|
|
identityProvider: IdentityProvider.CAL,
|
|
password: { create: { hash: hashedPassword } },
|
|
organizationId,
|
|
},
|
|
select: { id: true },
|
|
});
|
|
} catch (error) {
|
|
if (isPrismaError(error) && error.code === "P2002") {
|
|
const target = String(error.meta?.target ?? "");
|
|
if (target.includes("email") || target.includes("username")) {
|
|
return NextResponse.json({ message: SIGNUP_ERROR_CODES.USER_ALREADY_EXISTS }, { status: 409 });
|
|
}
|
|
}
|
|
throw error;
|
|
}
|
|
|
|
await createOrUpdateMemberships({
|
|
user,
|
|
team,
|
|
});
|
|
|
|
// Accept any child team invites for orgs.
|
|
if (team.parent) {
|
|
await joinAnyChildTeamOnOrgInvite({
|
|
userId: user.id,
|
|
org: team.parent,
|
|
});
|
|
}
|
|
}
|
|
|
|
// Cleanup token after use
|
|
await prisma.verificationToken.delete({
|
|
where: {
|
|
id: foundToken.id,
|
|
},
|
|
});
|
|
} else {
|
|
const isUsernameAvailable = !(await isUsernameReservedDueToMigration(correctedUsername));
|
|
if (!isUsernameAvailable) {
|
|
return NextResponse.json({ message: "A user exists with that username" }, { status: 409 });
|
|
}
|
|
if (IS_PREMIUM_USERNAME_ENABLED) {
|
|
const checkUsername = await checkPremiumUsername(correctedUsername);
|
|
if (checkUsername.premium) {
|
|
return NextResponse.json(
|
|
{ message: "Sign up from https://cal.com/signup to claim your premium username" },
|
|
{ status: 422 }
|
|
);
|
|
}
|
|
}
|
|
|
|
try {
|
|
await prisma.user.create({
|
|
data: {
|
|
username: correctedUsername,
|
|
email: userEmail,
|
|
password: { create: { hash: hashedPassword } },
|
|
identityProvider: IdentityProvider.CAL,
|
|
},
|
|
select: { id: true },
|
|
});
|
|
} catch (error) {
|
|
// Fallback for race conditions where user was created between our check and create
|
|
if (isPrismaError(error) && error.code === "P2002") {
|
|
const target = String(error.meta?.target ?? "");
|
|
if (target.includes("email") || target.includes("username")) {
|
|
return NextResponse.json({ message: SIGNUP_ERROR_CODES.USER_ALREADY_EXISTS }, { status: 409 });
|
|
}
|
|
}
|
|
throw error;
|
|
}
|
|
|
|
if (process.env.AVATARAPI_USERNAME && process.env.AVATARAPI_PASSWORD) {
|
|
await prefillAvatar({ email: userEmail });
|
|
}
|
|
|
|
await sendEmailVerification({
|
|
email: userEmail,
|
|
username: correctedUsername,
|
|
language,
|
|
});
|
|
}
|
|
|
|
return NextResponse.json({ message: "Created user" }, { status: 201 });
|
|
}
|