import React, { useState, useEffect, useRef } from 'react';
import { useAuth } from '../store/AuthContext';
import { useNavigate } from 'react-router-dom';
import { ShieldCheck, Mail, Lock, Sparkles, ArrowRight, Eye, EyeOff, RefreshCw } from 'lucide-react';
/* ── Floating Particle ─────────────────────────────────────── */
interface Particle {
id: number;
x: number;
y: number;
size: number;
opacity: number;
delay: number;
duration: number;
}
const generateParticles = (count: number): Particle[] =>
Array.from({ length: count }, (_, i) => ({
id: i,
x: Math.random() * 100,
y: Math.random() * 100,
size: Math.random() * 4 + 2,
opacity: Math.random() * 0.4 + 0.1,
delay: Math.random() * 6,
duration: Math.random() * 4 + 5,
}));
/* ── Animated Background ───────────────────────────────────── */
const AnimatedBackground: React.FC = () => {
const [particles] = useState(() => generateParticles(25));
return (
{/* Mesh gradient base */}
{/* Large glowing orbs */}
{/* Morphing blob */}
{/* Grid overlay */}
{/* Floating particles */}
{particles.map(p => (
))}
);
};
/* ── 3D Orbiting Ring ──────────────────────────────────────── */
const OrbitingRing: React.FC = () => (
{/* Center icon */}
{/* Orbiting ring 1 */}
{/* Orbiting ring 2 */}
);
/* ── Main LoginForm ────────────────────────────────────────── */
export const LoginForm: React.FC = () => {
const { login, register, verifyMfa, mfaPendingEmail } = useAuth();
const navigate = useNavigate();
const [isRegister, setIsRegister] = useState(false);
const [email, setEmail] = useState('');
const [showPass, setShowPass] = useState(false);
const [otp, setOtp] = useState('');
const [error, setError] = useState('');
const [loading, setLoading] = useState(false);
const [mousePos, setMousePos] = useState({ x: 0, y: 0 });
const cardRef = useRef(null);
// Client Profile Registration States
const [companyName, setCompanyName] = useState('');
const [website, setWebsite] = useState('');
const [sector, setSector] = useState('Technology');
const [companySize, setCompanySize] = useState('1-10');
/* 3D tilt effect on card */
const handleMouseMove = (e: React.MouseEvent) => {
if (!cardRef.current) return;
const rect = cardRef.current.getBoundingClientRect();
const x = (e.clientX - rect.left) / rect.width - 0.5;
const y = (e.clientY - rect.top) / rect.height - 0.5;
setMousePos({ x, y });
};
const handleMouseLeave = () => setMousePos({ x: 0, y: 0 });
const cardTransform = `perspective(1200px) rotateY(${mousePos.x * 8}deg) rotateX(${-mousePos.y * 6}deg) translateZ(0)`;
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setError('');
setLoading(true);
if (!email) { setError('Email address is required.'); setLoading(false); return; }
try {
if (isRegister) {
if (!companyName.trim() || !website.trim()) {
setError('Please fill in all company information.');
setLoading(false);
return;
}
await register(email, { companyName, website, sector, companySize });
} else {
await login(email);
}
} catch (err: unknown) {
setError(err instanceof Error ? err.message : 'Authentication failed.');
} finally {
setLoading(false);
}
};
const handleMfaSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setError('');
setLoading(true);
if (otp.length !== 6) { setError('Enter the 6-digit verification code.'); setLoading(false); return; }
try {
const loggedUser = await verifyMfa(otp);
navigate(loggedUser.role === 'ADMIN' ? '/admin' : '/client');
} catch (err: unknown) {
setError(err instanceof Error ? err.message : 'MFA validation failed.');
} finally {
setLoading(false);
}
};
const [mounted, setMounted] = useState(false);
useEffect(() => { setTimeout(() => setMounted(true), 50); }, []);
return (
{/* Card wrapper with 3D tilt */}
{/* Glow behind card */}
{/* Main glass card */}
{/* Top accent bar */}
{/* Brand Header */}
{mfaPendingEmail ? 'Verify Identity' : 'Tech4Biz Portal'}
{mfaPendingEmail
? `Code sent to ${mfaPendingEmail}`
: 'Enterprise hardware & software asset distribution'}
{/* Mode toggle */}
{!mfaPendingEmail && (
{(['Login', 'Sign Up'] as const).map(label => {
const active = label === 'Login' ? !isRegister : isRegister;
return (
);
})}
)}
{/* Error message */}
{error && (
)}
{/* ── Credentials Form ── */}
{!mfaPendingEmail ? (
) : (
/* ── MFA Form ── */
)}
{/* Bottom accent */}
© 2026 Tech4Biz Solutions Inc. · Enterprise Security Portal
);
};