chore: migrate signup API to appDir (#19975)
* refactor calcomHandler * refactor selfHostedHandler * fix * migrate to appdir * fix type checks
This commit is contained in:
1 parent
109c303b60
commit
1277f5e9f7
4 files changed
+89
-97
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import type { NextApiResponse } from "next";
|
||||
import { cookies, headers } from "next/headers";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
import { getPremiumMonthlyPlanPriceId } from "@calcom/app-store/stripepayment/lib/utils";
|
||||
import { hashPassword } from "@calcom/features/auth/lib/hashPassword";
|
||||
@@ -11,12 +12,14 @@ import { WEBAPP_URL } from "@calcom/lib/constants";
|
||||
import { getLocaleFromRequest } from "@calcom/lib/getLocaleFromRequest";
|
||||
import { HttpError } from "@calcom/lib/http-error";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { usernameHandler, type RequestWithUsernameStatus } from "@calcom/lib/server/username";
|
||||
import type { CustomNextApiHandler } from "@calcom/lib/server/username";
|
||||
import { usernameHandler } from "@calcom/lib/server/username";
|
||||
import { validateAndGetCorrectedUsernameAndEmail } from "@calcom/lib/validateUsername";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import { CreationSource } from "@calcom/prisma/enums";
|
||||
import { IdentityProvider } from "@calcom/prisma/enums";
|
||||
import { signupSchema } from "@calcom/prisma/zod-utils";
|
||||
import { buildLegacyRequest } from "@calcom/web/lib/buildLegacyCtx";
|
||||
|
||||
import { joinAnyChildTeamOnOrgInvite } from "../utils/organization";
|
||||
import {
|
||||
@@ -27,7 +30,7 @@ import {
|
||||
|
||||
const log = logger.getSubLogger({ prefix: ["signupCalcomHandler"] });
|
||||
|
||||
async function handler(req: RequestWithUsernameStatus, res: NextApiResponse) {
|
||||
const handler: CustomNextApiHandler = async (body, usernameStatus) => {
|
||||
const {
|
||||
email: _email,
|
||||
password,
|
||||
@@ -38,7 +41,7 @@ async function handler(req: RequestWithUsernameStatus, res: NextApiResponse) {
|
||||
password: true,
|
||||
token: true,
|
||||
})
|
||||
.parse(req.body);
|
||||
.parse(body);
|
||||
|
||||
const billingService = new StripeBillingService();
|
||||
|
||||
@@ -46,12 +49,12 @@ async function handler(req: RequestWithUsernameStatus, res: NextApiResponse) {
|
||||
|
||||
log.debug("handler", { email: _email });
|
||||
|
||||
let username: string | null = req.usernameStatus.requestedUserName;
|
||||
let username: string | null = usernameStatus.requestedUserName;
|
||||
let checkoutSessionId: string | null = null;
|
||||
|
||||
// Check for premium username
|
||||
if (req.usernameStatus.statusCode === 418) {
|
||||
return res.status(req.usernameStatus.statusCode).json(req.usernameStatus.json);
|
||||
if (usernameStatus.statusCode === 418) {
|
||||
return NextResponse.json(usernameStatus.json, { status: 418 });
|
||||
}
|
||||
|
||||
// Validate the user
|
||||
@@ -110,7 +113,7 @@ async function handler(req: RequestWithUsernameStatus, res: NextApiResponse) {
|
||||
const returnUrl = `${WEBAPP_URL}/api/integrations/stripepayment/paymentCallback?checkoutSessionId={CHECKOUT_SESSION_ID}&callbackUrl=/auth/verify?sessionId={CHECKOUT_SESSION_ID}`;
|
||||
|
||||
// Pro username, must be purchased
|
||||
if (req.usernameStatus.statusCode === 402) {
|
||||
if (usernameStatus.statusCode === 402) {
|
||||
const checkoutSession = await billingService.createSubscriptionCheckout({
|
||||
mode: "subscription",
|
||||
customerId: customer.stripeCustomerId,
|
||||
@@ -207,20 +210,23 @@ async function handler(req: RequestWithUsernameStatus, res: NextApiResponse) {
|
||||
}
|
||||
sendEmailVerification({
|
||||
email,
|
||||
language: await getLocaleFromRequest(req),
|
||||
language: await getLocaleFromRequest(buildLegacyRequest(await headers(), await cookies())),
|
||||
username: username || "",
|
||||
});
|
||||
}
|
||||
|
||||
if (checkoutSessionId) {
|
||||
console.log("Created user but missing payment", checkoutSessionId);
|
||||
return res.status(402).json({
|
||||
message: "Created user but missing payment",
|
||||
checkoutSessionId,
|
||||
});
|
||||
return NextResponse.json(
|
||||
{ message: "Created user but missing payment", checkoutSessionId },
|
||||
{ status: 402 }
|
||||
);
|
||||
}
|
||||
|
||||
return res.status(201).json({ message: "Created user", stripeCustomerId: customer.stripeCustomerId });
|
||||
}
|
||||
return NextResponse.json(
|
||||
{ message: "Created user", stripeCustomerId: customer.stripeCustomerId },
|
||||
{ status: 201 }
|
||||
);
|
||||
};
|
||||
|
||||
export default usernameHandler(handler);
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
import { checkPremiumUsername } from "@calcom/ee/common/lib/checkPremiumUsername";
|
||||
import { hashPassword } from "@calcom/features/auth/lib/hashPassword";
|
||||
@@ -21,16 +21,14 @@ import {
|
||||
validateAndGetCorrectedUsernameForTeam,
|
||||
} from "../utils/token";
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const data = req.body;
|
||||
const { email, password, language, token } = signupSchema.parse(data);
|
||||
export default async function handler(body: Record<string, string>) {
|
||||
const { email, password, language, token } = signupSchema.parse(body);
|
||||
|
||||
const username = slugify(data.username);
|
||||
const username = slugify(body.username);
|
||||
const userEmail = email.toLowerCase();
|
||||
|
||||
if (!username) {
|
||||
res.status(422).json({ message: "Invalid username" });
|
||||
return;
|
||||
return NextResponse.json({ message: "Invalid username" }, { status: 422 });
|
||||
}
|
||||
|
||||
let foundToken: { id: number; teamId: number | null; expires: Date } | null = null;
|
||||
@@ -52,10 +50,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
});
|
||||
if (!userValidation.isValid) {
|
||||
logger.error("User validation failed", { userValidation });
|
||||
return res.status(409).json({ message: "Username or email is already taken" });
|
||||
return NextResponse.json({ message: "Username or email is already taken" }, { status: 409 });
|
||||
}
|
||||
if (!userValidation.username) {
|
||||
return res.status(422).json({ message: "Invalid username" });
|
||||
return NextResponse.json({ message: "Invalid username" }, { status: 422 });
|
||||
}
|
||||
correctedUsername = userValidation.username;
|
||||
}
|
||||
@@ -86,8 +84,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
if (isCheckingUsernameInGlobalNamespace) {
|
||||
const isUsernameAvailable = !(await isUsernameReservedDueToMigration(correctedUsername));
|
||||
if (!isUsernameAvailable) {
|
||||
res.status(409).json({ message: "A user exists with that username" });
|
||||
return;
|
||||
return NextResponse.json({ message: "A user exists with that username" }, { status: 409 });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,16 +132,15 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
} else {
|
||||
const isUsernameAvailable = !(await isUsernameReservedDueToMigration(correctedUsername));
|
||||
if (!isUsernameAvailable) {
|
||||
res.status(409).json({ message: "A user exists with that username" });
|
||||
return;
|
||||
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) {
|
||||
res.status(422).json({
|
||||
message: "Sign up from https://cal.com/signup to claim your premium username",
|
||||
});
|
||||
return;
|
||||
return NextResponse.json(
|
||||
{ message: "Sign up from https://cal.com/signup to claim your premium username" },
|
||||
{ status: 422 }
|
||||
);
|
||||
}
|
||||
}
|
||||
await prisma.user.upsert({
|
||||
@@ -179,5 +175,5 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
});
|
||||
}
|
||||
|
||||
res.status(201).json({ message: "Created user" });
|
||||
return NextResponse.json({ message: "Created user" }, { status: 201 });
|
||||
}
|
||||
Reference in new issue
Block a user