"use server"; import { parseWithZod } from "@conform-to/zod"; import { signInSchema } from "~/schema/auth"; import { signInEmail } from "~/server/auth"; import { APIError } from "better-auth/api"; export async function signIn(prevState: unknown, formData: FormData) { const submission = parseWithZod(formData, { schema: signInSchema }); if (submission.status !== "success") { return submission.reply(); } try { await signInEmail({ body: { ...submission.value, callbackURL: "/", }, }); } catch (error) { if (error instanceof APIError) { if (error.status === "TOO_MANY_REQUESTS") { return submission.reply({ formErrors: [error.message], }); } if (error.status === "FORBIDDEN") { return submission.reply({ formErrors: ["Verify your email before siging in"], }); } if (error.status === "UNAUTHORIZED") { return submission.reply({ fieldErrors: { email: ["Invalid Credentials"], password: ["Invalid Credentials"], }, }); } throw error; } throw error; } }