The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
"use server";
import { prisma } from "@/lib/prisma";
import { ratelimit } from "@/lib/upstash";
import { waitUntil } from "@vercel/functions";
import { flattenValidationErrors } from "next-safe-action";
import * as z from "zod/v4";
import { createId } from "../api/create-id";
import { shouldApplyRateLimit } from "../api/environment";
import { hashPassword } from "../auth/password";
import { signUpSchema } from "../zod/schemas/auth";
import { throwIfAuthenticated } from "./auth/throw-if-authenticated";
import { actionClient } from "./safe-action";
const schema = signUpSchema.extend({
code: z.string().min(6, "OTP must be 6 characters long."),
});
const MAX_OTP_ATTEMPTS = 5; // Block after 5 failed attempts
const OTP_LOCKOUT_DURATION = "24 h"; // Block for 24 hours
// Sign up a new user using email and password
export const createUserAccountAction = actionClient
.inputSchema(schema, {
handleValidationErrorsShape: async (ve) =>
flattenValidationErrors(ve).fieldErrors,
})
.use(throwIfAuthenticated)
.action(async ({ parsedInput }) => {
const { email, password, code } = parsedInput;
const signupAttemptKey = `signup:attempts:${email}`;
if (shouldApplyRateLimit) {
const { remaining: attemptsRemaining } = await ratelimit(
MAX_OTP_ATTEMPTS,
OTP_LOCKOUT_DURATION,
).getRemaining(signupAttemptKey);
if (attemptsRemaining <= 0) {
throw new Error(
"Too many failed attempts. You have to try again later.",
);
}
}
const verificationToken = await prisma.emailVerificationToken.findUnique({
where: {
identifier_token: {
identifier: email,
token: code,
},
},
});
if (!verificationToken) {
await ratelimit(MAX_OTP_ATTEMPTS, OTP_LOCKOUT_DURATION).limit(
signupAttemptKey,
);
throw new Error("Invalid verification code entered.");
}
if (verificationToken.expires && verificationToken.expires < new Date()) {
waitUntil(
prisma.emailVerificationToken.delete({
where: {
identifier: email,
token: code,
},
}),
);
throw new Error("The OTP has expired. Please request a new one.");
}
await prisma.emailVerificationToken.delete({
where: {
identifier: email,
token: code,
},
});
const user = await prisma.user.findUnique({
where: {
email,
},
});
if (!user) {
await prisma.user.create({
data: {
id: createId({ prefix: "user_" }),
email,
passwordHash: await hashPassword(password),
emailVerified: new Date(),
notificationPreferences: {
create: {},
},
},
});
}
});