import React, { useState, useEffect } from "react"; import { Box, TextField, Button, Typography } from "@mui/material"; export default function Login({ onLogin }) { const [users, setUsers] = useState([]); const [username, setUsername] = useState("admin"); const [password, setPassword] = useState(""); const [error, setError] = useState(""); useEffect(() => { fetch("/api/users") .then((res) => res.json()) .then((data) => setUsers(data.users || [])) .catch(() => setUsers([])); }, []); const sha512 = async (text) => { const encoder = new TextEncoder(); const data = encoder.encode(text); const hashBuffer = await crypto.subtle.digest("SHA-512", data); const hashArray = Array.from(new Uint8Array(hashBuffer)); return hashArray.map((b) => b.toString(16).padStart(2, "0")).join(""); }; const handleSubmit = async (e) => { e.preventDefault(); const user = users.find((u) => u.username === username); if (!user) { setError("Invalid username or password"); return; } const hash = await sha512(password); if (hash.toLowerCase() === (user.password || "").toLowerCase()) { setError(""); onLogin(username); } else { setError("Invalid username or password"); } }; return ( Borealis Logo Borealis - Automation Platform setUsername(e.target.value)} margin="normal" /> setPassword(e.target.value)} margin="normal" /> {error && ( {error} )} ); }