"use server"; import { checkInboxPath, SIGN_IN_PATH, signIn } from "@/auth"; import { redirect } from "next/navigation"; import { z } from "zod"; const emailSchema = z.string().trim().toLowerCase().pipe(z.email().max(254)); export async function requestSignInLink(formData: FormData) { const parsed = emailSchema.safeParse(formData.get("email")); if (!parsed.success) redirect(`${SIGN_IN_PATH}?error=InvalidEmail`); const email = parsed.data; // redirect: false hands back where Auth.js would have gone: the check-your- // inbox page (also when the throttle skipped the send) or ?error=. const next = await signIn("email", { email, redirectTo: "/account", redirect: false, }); const error = new URL( String(next), "http://schematax.invalid", ).searchParams.get("error"); redirect( error ? `${SIGN_IN_PATH}?${new URLSearchParams({ error })}` : checkInboxPath(email), ); }