The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Refactor account existence check to use server action
Kiran K798547de9eb9
diff --git a/apps/web/app/api/auth/account-exists/route.ts b/apps/web/app/api/auth/account-exists/route.tsdeleted file mode 100644index 7e9a82fbc8..0000000000--- a/apps/web/app/api/auth/account-exists/route.ts+++ /dev/null@@ -1,33 +0,0 @@-import { conn } from "@/lib/planetscale";-import { ratelimit } from "@/lib/upstash";-import { ipAddress } from "@vercel/functions";-import { NextRequest, NextResponse } from "next/server";--export const runtime = "edge";--export async function POST(req: NextRequest) {- const ip = ipAddress(req);- const { success } = await ratelimit(8, "1 m").limit(`account-exists:${ip}`);- if (!success) {- return new Response("Don't DDoS me pls 🥺", { status: 429 });- }-- const { email } = (await req.json()) as { email: string };-- if (!process.env.NEXT_PUBLIC_IS_DUB) {- return NextResponse.json({ accountExists: true, hasPassword: true });- }-- const user = await conn- .execute("SELECT email, passwordHash FROM User WHERE email = ?", [email])- .then((res) => res.rows[0]);-- if (user) {- return NextResponse.json({- accountExists: true,- hasPassword: !!user.passwordHash,- });- }-- return NextResponse.json({ accountExists: false, hasPassword: false });-}diff --git a/apps/web/lib/actions/check-account-exists.ts b/apps/web/lib/actions/check-account-exists.tsnew file mode 100644index 0000000000..953f097c4b--- /dev/null+++ b/apps/web/lib/actions/check-account-exists.ts@@ -0,0 +1,43 @@+"use server";++import { ratelimit } from "@/lib/upstash";+import { prisma } from "@dub/prisma";+import { getIP } from "../api/utils";+import z from "../zod";+import { emailSchema } from "../zod/schemas/auth";+import { throwIfAuthenticated } from "./auth/throw-if-authenticated";+import { actionClient } from "./safe-action";++const schema = z.object({+ email: emailSchema,+});++// Check if account exists+export const checkAccountExistsAction = actionClient+ .schema(schema)+ .use(throwIfAuthenticated)+ .action(async ({ parsedInput }) => {+ const { email } = parsedInput;++ const { success } = await ratelimit(8, "1 m").limit(+ `account-exists:${getIP()}`,+ );++ if (!success) {+ throw new Error("Too many requests. Please try again later.");+ }++ const user = await prisma.user.findUnique({+ where: {+ email,+ },+ select: {+ passwordHash: true,+ },+ });++ return {+ accountExists: !!user,+ hasPassword: !!user?.passwordHash,+ };+ });