import * as React from 'react'; import { Box, TextField, Button, Typography, IconButton, CircularProgress, Link, InputAdornment, alpha, } from '@mui/material'; import VisibilityIcon from '@mui/icons-material/Visibility'; import VisibilityOffIcon from '@mui/icons-material/VisibilityOff'; import CheckRoundedIcon from '@mui/icons-material/CheckRounded'; import ArrowBackRoundedIcon from '@mui/icons-material/ArrowBackRounded'; import { useTheme } from '@mui/material/styles'; export type AuthMode = "login" | "register"; export interface AuthPageProps { mode: AuthMode; onBack(): void; onSwitchMode(): void; login(username: string, password: string): Promise; register(username: string, password: string): Promise; loading: boolean; error: string | null; currentUser: any; } const FEATURES = [ "OpenAPI-driven admin for accounts, expenses, tags & payors", "Import bank statements with live pipeline progress", "Entity & tag enrichment for clean, comparable data", ]; export function AuthPage({ mode, onBack, onSwitchMode, login, register, loading, error, currentUser, }: AuthPageProps) { const theme = useTheme(); const [username, setUsername] = React.useState(''); const [password, setPassword] = React.useState(''); const [showPassword, setShowPassword] = React.useState(false); const [fieldErrors, setFieldErrors] = React.useState<{ username?: string; password?: string }>({}); const isLogin = mode === "login"; React.useEffect(() => { if (currentUser) onBack(); }, [currentUser, onBack]); const validate = (): boolean => { const next: { username?: string; password?: string } = {}; if (!username.trim()) next.username = "Username is required"; else if (username.trim().length < 3) next.username = "Username must be at least 3 characters"; if (!password) next.password = "Password is required"; else if (password.length < 6) next.password = "Password must be at least 6 characters"; setFieldErrors(next); return Object.keys(next).length === 0; }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!validate()) return; if (isLogin) { await login(username.trim(), password); } else { await register(username.trim(), password); } }; const handleSwitch = () => { setFieldErrors({}); onSwitchMode(); }; const form = ( {isLogin ? "Sign in to Khata" : "Create your account"} {isLogin ? "Welcome back. Enter your credentials to continue." : "Start managing your ledger in a few seconds."}
{ setUsername(e.target.value); if (fieldErrors.username) setFieldErrors((p) => ({ ...p, username: undefined })); }} error={!!fieldErrors.username} helperText={fieldErrors.username} autoComplete="username" autoFocus /> { setPassword(e.target.value); if (fieldErrors.password) setFieldErrors((p) => ({ ...p, password: undefined })); }} error={!!fieldErrors.password} helperText={fieldErrors.password} autoComplete={isLogin ? 'current-password' : 'new-password'} InputProps={{ endAdornment: ( setShowPassword((s) => !s)} edge="end" size="small" sx={{ border: 'none' }} > {showPassword ? : } ), }} /> {error && ( {error} )}
{isLogin ? "Don't have an account?" : "Already have an account?"}{' '} {isLogin ? "Register" : "Sign In"}
); return ( `0 12px 40px ${alpha(t.palette.common.black, t.palette.mode === 'dark' ? 0.4 : 0.08)}`, }} > {/* LEFT BRAND PANEL */} Khata The intelligent, extensible financial ledger for your cashflow. {FEATURES.map((f) => ( {f} ))} {/* RIGHT FORM PANEL */} {form} ); }