import { zodResolver } from "@hookform/resolvers/zod"; import { useForm } from "react-hook-form"; import { FaGoogle } from "react-icons/fa"; import { z } from "zod"; import { authClient } from "@kan/auth"; import Button from "~/components/Button"; import Input from "~/components/Input"; import { api } from "~/utils/api"; 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), }); const email = watch("email"); const loginWithEmail = api.auth.loginWithEmail.useMutation({ onSuccess: () => { setIsMagicLinkSent(true, email); }, }); const handleLoginWithEmail = async (email: string) => { const { data, error } = await authClient.signIn.magicLink({ email, callbackURL: "/boards", }); if (!error) { setIsMagicLinkSent(true, email); } }; const onSubmit = async (values: FormValues) => { await handleLoginWithEmail(values.email); }; return (
or
{!loginWithEmail.error && errors.email && (

Please enter a valid email address

)} {loginWithEmail.error ? (

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

) : null}
); }