"use client"; import { Button } from "@/components/ui/button"; import { Card, CardContent } from "@/components/ui/card"; import { Field, FieldDescription, FieldGroup, FieldLabel, FieldSeparator, } from "@/components/ui/field"; import { Input } from "@/components/ui/input"; import { authClient, googleSignInAvailable } from "@/lib/auth/client"; import { cn } from "@/lib/utils/ui"; import { RiLoader4Line } from "@remixicon/react"; import { useRouter } from "next/navigation"; import { useState, type FormEvent } from "react"; import { toast } from "sonner"; import { Logo } from "../logo"; import { AuthErrorAlert } from "./auth-error-alert"; import { AuthHeader } from "./auth-header"; import AuthSidebar from "./auth-sidebar"; import { GoogleAuthButton } from "./google-auth-button"; type DivProps = React.ComponentProps<"div">; export function SignupForm({ className, ...props }: DivProps) { const router = useRouter(); const isGoogleAvailable = googleSignInAvailable; const [fullname, setFullname] = useState(""); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [error, setError] = useState(""); const [loadingEmail, setLoadingEmail] = useState(false); const [loadingGoogle, setLoadingGoogle] = useState(false); async function handleSubmit(e: FormEvent) { e.preventDefault(); await authClient.signUp.email( { email, password, name: fullname, }, { onRequest: () => { setError(""); setLoadingEmail(true); }, onSuccess: () => { setLoadingEmail(false); toast.success("Conta criada com sucesso!"); router.replace("/dashboard"); }, onError: (ctx) => { setError(ctx.error.message); setLoadingEmail(false); }, } ); } async function handleGoogle() { if (!isGoogleAvailable) { setError("Login com Google não está disponível no momento."); return; } // Ativa loading antes de iniciar o fluxo OAuth setError(""); setLoadingGoogle(true); // OAuth redirect - o loading permanece até a página ser redirecionada await authClient.signIn.social( { provider: "google", callbackURL: "/dashboard", }, { onError: (ctx) => { // Só desativa loading se houver erro setError(ctx.error.message); setLoadingGoogle(false); }, } ); } return (
Nome completo setFullname(e.target.value)} aria-invalid={!!error} /> E-mail setEmail(e.target.value)} aria-invalid={!!error} /> Senha setPassword(e.target.value)} aria-invalid={!!error} /> Ou continue com Já tem uma conta?{" "} Entrar
{/* */} Voltar para o site
); }