import { useForm } from "react-hook-form"; import { api } from "~/utils/api"; import { FaGoogle } from "react-icons/fa"; import { z } from "zod"; import { zodResolver } from "@hookform/resolvers/zod"; import LoadingSpinner from "~/components/LoadingSpinner"; interface FormValues { email: string; } interface AuthProps { setIsMagicLinkSent: (value: boolean, recipient: string) => void; } const EmailSchema = z.object({ email: z.string().email() }); export function Auth({ setIsMagicLinkSent }: AuthProps) { const { register, handleSubmit, watch, formState: { errors }, } = useForm({ resolver: zodResolver(EmailSchema), // Apply the zodResolver }); const email = watch("email"); const loginWithEmail = api.auth.loginWithEmail.useMutation({ onSuccess: () => { setIsMagicLinkSent(true, email); }, }); const loginWithOAuth = api.auth.loginWithOAuth.useMutation({ onSuccess: (data) => { if (data?.url) window.open(data.url); }, }); const onSubmit = (values: FormValues) => { try { loginWithEmail.mutate({ email: values.email, }); } catch (error) { console.error(error); } }; return (
{!loginWithEmail.error && !loginWithOAuth.error && errors.email && (

Please enter a valid email address

)} {loginWithEmail.error ?? loginWithOAuth.error ? (

Something went wrong, please try again later or contact customer support.

) : null}
); }