import { useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { KeyRound, ShieldAlert } from 'lucide-react'; export function AdminLogin() { const [password, setPassword] = useState(''); const [error, setError] = useState(''); const navigate = useNavigate(); const handleLogin = async (e) => { e.preventDefault(); try { // Encrypt password securely on client before sending const msgUint8 = new TextEncoder().encode(password); const hashBuffer = await crypto.subtle.digest('SHA-256', msgUint8); const hashArray = Array.from(new Uint8Array(hashBuffer)); const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join(''); const res = await fetch(`/api/admin/login`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ passwordHash: hashHex }) }); const data = await res.json(); if (res.ok) { sessionStorage.setItem('adminToken', data.token); navigate('/admin/dashboard'); } else { setError(data.error || 'Login failed'); } } catch { setError('Server unreachable'); } }; return (