import * as React from "react"; import { Eye, EyeOff, Loader2, Lock, Mail, Phone } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Form, FormField } from "@/components/ui/form"; import { cn } from "@/lib/cn"; import { isValidEmail, isValidIndonesianPhone } from "@/lib/utils"; /** * LoginScreen - UI shell for the login page. Callback adapter for * each backend action. Does NOT include authentication logic. * * Common adapters: * - auth.js / NextAuth: handled on the server via route handler. * - Supabase: signInWithPassword in onSubmit. * - Custom: fetch to endpoint. * * Example: * { * await signIn(identifier, secret); * }} * socialProviders={[ * { id: "google", label: "Google", onClick: () => signInWithGoogle() }, * ]} * /> */ export type IdentifierType = "email" | "phone" | "both"; export type SecretType = "password" | "pin"; export interface SocialProvider { id: string; label: string; icon?: React.ReactNode; onClick: () => void; } export interface LoginScreenProps { /** Large title (default "Log in"). */ title?: string; /** Subtitle / short description. */ subtitle?: string; /** Identifier (email/phone) or both. Default "both". */ identifierType?: IdentifierType; /** "password" (default) or "pin" (numeric). */ secretType?: SecretType; /** Identifier placeholder. */ identifierLabel?: string; /** Secret placeholder. */ secretLabel?: string; /** Submit handler. Throw or return {error} to show an error. */ onSubmit?: (data: { identifier: string; secret: string; remember: boolean; }) => Promise; /** Social providers (Google, Apple, Facebook, etc). */ socialProviders?: SocialProvider[]; /** External loading state. */ loading?: boolean; /** External error (overrides internal). */ error?: string; /** Footer slot (link "Don't have an account? Sign up" etc). */ footer?: React.ReactNode; /** ClassName for the wrapper. */ className?: string; } const phoneHint = "Example: 081234567890"; export function LoginScreen({ title = "Log in", subtitle = "Continue to your account.", identifierType = "both", secretType = "password", identifierLabel, secretLabel, onSubmit, socialProviders, loading: externalLoading, error: externalError, footer, className, }: LoginScreenProps) { const [identifier, setIdentifier] = React.useState(""); const [secret, setSecret] = React.useState(""); const [showSecret, setShowSecret] = React.useState(false); const [remember, setRemember] = React.useState(true); const [internalLoading, setInternalLoading] = React.useState(false); const [internalError, setInternalError] = React.useState( undefined ); const loading = externalLoading ?? internalLoading; const error = externalError ?? internalError; const identifierInputType = identifierType === "email" ? "email" : identifierType === "phone" ? "tel" : "text"; const autoLabel = identifierLabel ?? (identifierType === "email" ? "Email" : identifierType === "phone" ? "Phone number" : "Email or phone number"); const autoSecretLabel = secretLabel ?? (secretType === "pin" ? "PIN" : "Password"); const validateIdentifier = (): string | undefined => { const v = identifier.trim(); if (!v) return "Required."; if (identifierType === "email") { return isValidEmail(v) ? undefined : "Invalid email format."; } if (identifierType === "phone") { return isValidIndonesianPhone(v) ? undefined : "Invalid phone number."; } // both if (v.includes("@")) { return isValidEmail(v) ? undefined : "Invalid email format."; } return isValidIndonesianPhone(v) ? undefined : "Invalid email or phone number."; }; const validateSecret = (): string | undefined => { if (!secret) return "Required."; if (secretType === "pin" && !/^\d{4,8}$/.test(secret)) { return "PIN must be 4-8 digits."; } return undefined; }; const handleSubmit = async (): Promise => { if (loading) return; setInternalError(undefined); if (externalLoading === undefined) { setInternalLoading(true); try { if (onSubmit) { const res = await onSubmit({ identifier, secret, remember }); if (res && "error" in res && res.error) { setInternalError(res.error); } } } catch (err) { setInternalError( err instanceof Error ? err.message : "An unexpected error occurred." ); } finally { setInternalLoading(false); } } else if (onSubmit) { try { const res = await onSubmit({ identifier, secret, remember }); if (res && "error" in res && res.error) setInternalError(res.error); } catch (err) { setInternalError( err instanceof Error ? err.message : "An unexpected error occurred." ); } } }; const Icon = identifierInputType === "email" ? Mail : identifierInputType === "tel" ? Phone : Mail; const LockIcon = Lock; return (

{title}

{subtitle && (

{subtitle}

)}
{error && (
{error}
)} {(ctx) => (
setIdentifier(e.target.value)} placeholder={ identifierType === "email" ? "name@example.com" : identifierType === "phone" ? "081234567890" : "name@example.com or 081234567890" } className="pl-10" disabled={loading} />
)}
{(ctx) => (
setSecret(e.target.value)} placeholder="••••••••" className="px-10" disabled={loading} />
)}
{socialProviders && socialProviders.length > 0 && (
or
{socialProviders.map((p) => ( ))}
)} {footer &&
{footer}
}
); }