"use client"; import { zodResolver } from "@hookform/resolvers/zod"; import { Link } from "@nextui-org/react"; import { useRouter } from "next/navigation"; import { useForm } from "react-hook-form"; import { z } from "zod"; import { authenticate, createNewUser } from "@/actions/auth"; import { NotificationIcon, ProwlerExtended } from "@/components/icons"; import { ThemeSwitch } from "@/components/ThemeSwitch"; import { useToast } from "@/components/ui"; import { CustomButton, CustomInput } from "@/components/ui/custom"; import { Form } from "@/components/ui/form"; import { ApiError, authFormSchema } from "@/types"; export const AuthForm = ({ type, invitationToken, isCloudEnv, }: { type: string; invitationToken?: string | null; isCloudEnv?: boolean; }) => { const formSchema = authFormSchema(type); const router = useRouter(); const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { email: "", password: "", ...(type === "sign-up" && { name: "", company: "", confirmPassword: "", ...(invitationToken && { invitationToken }), }), }, }); const isLoading = form.formState.isSubmitting; const { toast } = useToast(); const onSubmit = async (data: z.infer) => { if (type === "sign-in") { const result = await authenticate(null, { email: data.email.toLowerCase(), password: data.password, }); if (result?.message === "Success") { router.push("/"); } else if (result?.errors && "credentials" in result.errors) { form.setError("email", { type: "server", message: result.errors.credentials ?? "Incorrect email or password", }); } else if (result?.message === "User email is not verified") { router.push("/email-verification"); } else { toast({ variant: "destructive", title: "Oops! Something went wrong", description: "An unexpected error occurred. Please try again.", }); } } if (type === "sign-up") { const newUser = await createNewUser(data); if (!newUser.errors) { toast({ title: "Success!", description: "The user was registered successfully.", }); form.reset(); if (isCloudEnv) { router.push("/email-verification"); } else { router.push("/sign-in"); } } else { newUser.errors.forEach((error: ApiError) => { const errorMessage = error.detail; switch (error.source.pointer) { case "/data/attributes/name": form.setError("name", { type: "server", message: errorMessage }); break; case "/data/attributes/email": form.setError("email", { type: "server", message: errorMessage }); break; case "/data/attributes/company_name": form.setError("company", { type: "server", message: errorMessage, }); break; case "/data/attributes/password": form.setError("password", { type: "server", message: errorMessage, }); break; case "/data": form.setError("invitationToken", { type: "server", message: errorMessage, }); break; default: toast({ variant: "destructive", title: "Oops! Something went wrong", description: errorMessage, }); } }); } } }; return (
{/* Auth Form */}
{/* Background Pattern */}
{/* Prowler Logo */}

{type === "sign-in" ? "Sign In" : "Sign Up"}

{type === "sign-up" && ( <> )} {/* {type === "sign-in" && (
Remember me Forgot password?
)} */} {type === "sign-up" && ( <> {invitationToken && ( )} )} {form.formState.errors?.email && (

No user found

)} {isLoading ? ( Loading ) : ( {type === "sign-in" ? "Log In" : "Sign Up"} )} {/* {type === "sign-in" && ( <>

OR

)} */} {type === "sign-in" ? (

Need to create an account?  Sign Up

) : (

Already have an account?  Log In

)}
); };