import React, { useState } from "react"; import { Alert, Button, Input, Spin } from "antd"; import { LockOutlined, MailOutlined } from "@ant-design/icons"; import { rpc } from "juava"; import { AuthType } from "./SignInOrUp"; type AuthMethod = { type: AuthType; oidcProviderId?: string; oidcProviderName?: string; }; interface EmailFirstLoginProps { onPasswordLogin: (email: string, password: string, type: AuthType) => Promise; onSSOLogin: (type: AuthType, providerId: string, loginHint?: string) => Promise; signup?: boolean; // Optional prop to indicate if this is a signup flow } export const EmailFirstLogin: React.FC = ({ onPasswordLogin, onSSOLogin, signup }) => { const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [authMethod, setAuthMethod] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [checkingEmail, setCheckingEmail] = useState(false); const checkAuthMethod = async () => { if (!email || !email.includes("@")) { setError("Please enter a valid email address"); return; } setCheckingEmail(true); setError(null); try { const result = await rpc("/api/auth/check-email", { body: { email }, }); const authMethod = result[0]; setAuthMethod(authMethod); // Auto-trigger social login if applicable switch (authMethod.type) { case "firebase-google": case "firebase-github": case "nextauth-github": case "nextauth-oidc": await onSSOLogin(authMethod.type, authMethod.type, email); break; case "dynamic-oidc": await onSSOLogin(authMethod.type, authMethod.oidcProviderId, email); } } catch (err: any) { setError("Failed to check authentication method. Please try again."); console.error("Error checking auth method:", err); } finally { setCheckingEmail(false); } }; const handlePasswordLogin = async () => { if (!email || !password) { setError("Please enter both email and password"); return; } setLoading(true); setError(null); try { await onPasswordLogin(email, password, authMethod?.type!); } catch (err: any) { setError(err.message || "Login failed. Please check your credentials."); } finally { setLoading(false); } }; const renderAuthForm = () => { if (!authMethod) { return null; } switch (authMethod.type) { case "firebase-password": case "nextauth-credentials": return (
Password
setPassword(e.target.value)} placeholder="Enter your password" onPressEnter={handlePasswordLogin} />
); case "firebase-google": return (
} />
); case "firebase-github": case "nextauth-github": return (
} />
); case "nextauth-oidc": return (
} />
); case "dynamic-oidc": return (
} />
); case "none": return (
); default: return null; } }; return (
Email
{ setEmail(e.target.value); setAuthMethod(null); setError(null); }} placeholder="Enter your email address" onPressEnter={checkAuthMethod} disabled={checkingEmail || loading} />
{!authMethod && ( )} {error && ( setError(null)} /> )} {renderAuthForm()}
); };