import { zodResolver } from "@hookform/resolvers/zod"; import { useState } from "react"; 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"; 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 [isLoginWithGooglePending, setIsLoginWithGooglePending] = useState(false); const [isLoginWithEmailPending, setIsLoginWithEmailPending] = useState(false); const [loginError, setLoginError] = useState(null); const { register, handleSubmit, formState: { errors }, } = useForm({ resolver: zodResolver(EmailSchema), }); const handleLoginWithEmail = async (email: string) => { setIsLoginWithEmailPending(true); setLoginError(null); const { error } = await authClient.signIn.magicLink({ email, callbackURL: "/boards", }); setIsLoginWithEmailPending(false); if (error) { setLoginError( "Something went wrong, please try again later or contact customer support.", ); } else { setIsMagicLinkSent(true, email); } }; const handleLoginWithGoogle = async () => { setIsLoginWithGooglePending(true); setLoginError(null); const { error } = await authClient.signIn.social({ provider: "google", callbackURL: "/boards", }); setIsLoginWithGooglePending(false); if (error) { setLoginError("Failed to login with Google. Please try again."); } }; const onSubmit = async (values: FormValues) => { await handleLoginWithEmail(values.email); }; return (
or
{errors.email && (

Please enter a valid email address

)} {loginError && (

{loginError}

)}
); }