diff --git a/components/auth/login-form.tsx b/components/auth/login-form.tsx index d69ff3e..dcfb8a8 100644 --- a/components/auth/login-form.tsx +++ b/components/auth/login-form.tsx @@ -36,38 +36,33 @@ export default function LoginForm() { return } - // Check if email exists by attempting sign-in with empty password setIsLoading(true) - const { error } = await authClient.signIn.email( - { email, password: "" }, - { - onError: () => { - // Silently handle - we check the error type below - }, - }, - ) - setIsLoading(false) + try { + const res = await fetch("/api/auth/check-email", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ email }), + }) - if (error) { - // If error is about invalid credentials, email exists but password is wrong - // If error is about user not found, email doesn't exist - if ( - error.message?.toLowerCase().includes("not found") || - error.message?.toLowerCase().includes("invalid email") || - error.status === 404 - ) { - setError( - "No account found with this email.", - ) + if (!res.ok) { + const data = await res.json() + setError(data.error || "Something went wrong. Please try again.") return } + + const data = await res.json() + if (!data.exists) { + setError("No account found with this email.") + return + } + // Email exists, move to password phase setPhase("password") - return + } catch { + setError("Something went wrong. Please try again.") + } finally { + setIsLoading(false) } - - // Shouldn't reach here with empty password, but handle it - setPhase("password") } const handleLogin = async (e: React.FormEvent) => { @@ -99,22 +94,22 @@ export default function LoginForm() { } return ( -
+
-

+

Welcome back

-

+

{phase === "email" ? "Sign in to DeckyVault" : `Signing in as `} {phase === "password" && ( <> - {email} + {email} {" "} @@ -126,22 +121,22 @@ export default function LoginForm() {

-
- +
+ or continue with email -
+
{error && ( -
+
{error} {error.includes("No account found") && ( <> {" "} Create one → @@ -153,7 +148,7 @@ export default function LoginForm() { {phase === "email" ? (
-
{/* Passkey hint */} -
-

+

+

Your browser may offer to sign in with a passkey

)} -

+

Don't have an account?{" "} Create one