diff --git a/apps/web/src/components/AuthForm.tsx b/apps/web/src/components/AuthForm.tsx index a2e41bcd..3b51b2b3 100644 --- a/apps/web/src/components/AuthForm.tsx +++ b/apps/web/src/components/AuthForm.tsx @@ -1,4 +1,5 @@ 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"; @@ -7,7 +8,6 @@ import { authClient } from "@kan/auth"; import Button from "~/components/Button"; import Input from "~/components/Input"; -import { api } from "~/utils/api"; interface FormValues { email: string; @@ -20,34 +20,53 @@ interface AuthProps { 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, - 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({ + setIsLoginWithEmailPending(true); + setLoginError(null); + const { error } = await authClient.signIn.magicLink({ email, callbackURL: "/boards", }); - if (!error) { + 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); }; @@ -56,12 +75,8 @@ export function Auth({ setIsMagicLinkSent }: AuthProps) {