import { useAuthActions } from "@convex-dev/auth/react"; import { useState } from "react"; import { useRouter, useSearchParams } from "next/navigation"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"; import { Label } from "@/components/ui/label"; import { Separator } from "@/components/ui/separator"; export function SignIn() { const { signIn } = useAuthActions(); const router = useRouter(); const searchParams = useSearchParams(); const nextPath = searchParams.get("next"); const [step, setStep] = useState<"signIn" | "signUp">("signIn"); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [error, setError] = useState(null); const handleSubmit = async (event: React.FormEvent) => { event.preventDefault(); setError(null); try { const flow = step === "signIn" ? "signIn" : "signUp"; await signIn("password", { email, password, flow }); const next = nextPath || (flow === "signIn" ? "/app/dashboard" : "/onboarding"); if (flow === "signIn") { router.push(next); } else { router.push(next); } } catch (err: any) { console.error(err); const errorMessage = err.message || "Error signing in. Please check your credentials."; if (errorMessage.includes("Account") && errorMessage.includes("already exists")) { setError("Account already exists. Please sign in instead."); } else { setError(errorMessage); } } }; const handleGoogleSignIn = () => { const next = nextPath || "/app/dashboard"; void signIn("google", { redirectTo: next }); }; return ( {step === "signIn" ? "Welcome back" : "Create an account"} {step === "signIn" ? "Enter your email to sign in to your account" : "Enter your email below to create your account"}
{error && (
{error}
)}
Or continue with email
setEmail(e.target.value)} required />
setPassword(e.target.value)} required minLength={8} />
); }