"use client"; //components/modales/settingsModal/SettingsModal.jsx import React, { useState, useEffect } from "react"; import ReactModal from "react-modal"; import { ClipLoader } from "react-spinners"; import "bootstrap-icons/font/bootstrap-icons.css"; import { useSelector, useDispatch } from "react-redux"; import handleClearDatabase from "./handlers/handleClearDatabase"; import handleReboot from "./handlers/handleReboot"; import handleSetDateTime from "./handlers/handleSetDateTime"; import handleSubmit from "./handlers/handleSubmit"; import { useRouter } from "next/router"; import bcrypt from "bcryptjs"; ReactModal.setAppElement("#__next"); const USERS = { Admin: { username: "admin", // Gehashte Version von "admin" mit bcrypt password: "$2a$10$xpq/.tcOJN/LXfzdCcCVrenlBh2nRlM1R1ISY7dd1q2qGWC9Fyd2G", role: "Admin", }, }; // Function to generate JWT token function generateToken(user) { const payload = { username: user.username, role: user.role, exp: Date.now() + 5 * 60 * 1000, // Ablaufzeit: 5 Minuten }; const token = JSON.stringify(payload); const encryptedToken = CryptoJS.AES.encrypt( token, process.env.NEXT_PUBLIC_ENCRYPTION_KEY ).toString(); return encryptedToken; } function decryptToken(encryptedToken) { const bytes = CryptoJS.AES.decrypt( encryptedToken, process.env.NEXT_PUBLIC_ENCRYPTION_KEY ); const decryptedToken = bytes.toString(CryptoJS.enc.Utf8); return JSON.parse(decryptedToken); } function SettingModal({ showModal, onClose }) { const [isAdminLoggedIn, setAdminLoggedIn] = useState(false); //const isAdminLoggedIn = sessionStorage.getItem("token"); const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [error, setError] = useState(""); const [showLoginForm, setShowLoginForm] = useState(false); // Zustand für Login-Formular const router = useRouter(); function handleAdminLogin(e) { e.preventDefault(); const user = USERS.Admin; // Finde den Admin-Benutzer bcrypt.compare(password, user.password, (err, isMatch) => { if (isMatch) { const token = generateToken(user); sessionStorage.setItem("token", token); // Speichere Token in SessionStorage localStorage.setItem("isAdminLoggedIn", "true"); setShowLoginForm(false); onClose(); } else { setError( "Login fehlgeschlagen. Bitte überprüfen Sie Benutzername und Passwort." ); } }); } const deviceName_Redux = useSelector((state) => state.variables.deviceName); const mac1_Redux = useSelector((state) => state.variables.mac1); const ip_Redux = useSelector((state) => state.variables.ip); const subnet_Redux = useSelector((state) => state.variables.subnet); const gateway_Redux = useSelector((state) => state.variables.gateway); const datetime_Redux = useSelector( (state) => state.variables.cplInternalTimestamp ); const ntp1_Redux = useSelector((state) => state.variables.ntp1); const ntp2_Redux = useSelector((state) => state.variables.ntp2); const ntp3_Redux = useSelector((state) => state.variables.ntp3); const ntpTimezone_Redux = useSelector((state) => state.variables.ntpTimezone); const active_Redux = useSelector((state) => state.variables.ntpActive); const [name, setName] = useState(deviceName_Redux || ""); const [mac1, setMac1] = useState(mac1_Redux || ""); const [ip, setIp] = useState(ip_Redux || ""); const [subnet, setSubnet] = useState(subnet_Redux || ""); const [gateway, setGateway] = useState(gateway_Redux || ""); const [systemUhr, setSystemUhr] = useState(datetime_Redux || ""); const [ntp1, setNtp1] = useState(ntp1_Redux || ""); const [ntp2, setNtp2] = useState(ntp2_Redux || ""); const [ntp3, setNtp3] = useState(ntp3_Redux || ""); const [ntpTimezone, setNtpTimezone] = useState(ntpTimezone_Redux || ""); const [active, setActive] = useState(active_Redux || ""); const [originalValues, setOriginalValues] = useState({}); const currentValues = { name, ip, subnet, gateway, ntp1, ntp2, ntp3, ntpTimezone, active, }; const handleAdminLogout = () => { sessionStorage.removeItem("token"); // Token aus sessionStorage entfernen localStorage.setItem("isAdminLoggedIn", "false"); // Admin-Status im localStorage setzen }; useEffect(() => { if (showModal) { setName(deviceName_Redux || ""); setMac1(mac1_Redux || ""); setIp(ip_Redux || ""); setSubnet(subnet_Redux || ""); setGateway(gateway_Redux || ""); setSystemUhr(datetime_Redux || ""); setNtp1(ntp1_Redux || ""); setNtp2(ntp2_Redux || ""); setNtp3(ntp3_Redux || ""); setNtpTimezone(ntpTimezone_Redux || ""); setActive(active_Redux || ""); } }, [showModal]); useEffect(() => { setOriginalValues({ name: deviceName_Redux, ip: ip_Redux, subnet: subnet_Redux, gateway: gateway_Redux, ntp1: ntp1_Redux, ntp2: ntp2_Redux, ntp3: ntp3_Redux, ntpTimezone: ntpTimezone_Redux, active: active_Redux, }); }, [ deviceName_Redux, ip_Redux, subnet_Redux, gateway_Redux, ntp1_Redux, ntp2_Redux, ntp3_Redux, ntpTimezone_Redux, active_Redux, ]); useEffect(() => { // Check if a valid token exists in localStorage const token = sessionStorage.getItem("token"); if (token) { setAdminLoggedIn(true); const { exp } = JSON.parse(atob(token)); if (Date.now() < exp) { setAdminLoggedIn(true); } else { // localStorage.removeItem("token"); // Remove expired token } } }, []); return ( <> {/* Hauptinhalt oder Login-Formular */} {showLoginForm ? (

Admin Login

e.preventDefault()}>
setUsername(e.target.value)} />
setPassword(e.target.value)} />
{error &&

{error}

}
) : (

System:

setName(e.target.value)} />
setMac1(e.target.value)} disabled />
setIp(e.target.value)} />
setSubnet(e.target.value)} />
setGateway(e.target.value)} />
{/* Button für Systemzeit übernehmen */}
{/* SNTP Client */}

SNTP Client:

setNtp1(e.target.value)} />
setNtp2(e.target.value)} />
setNtp3(e.target.value)} />
setNtpTimezone(e.target.value)} />
setActive(e.target.value)} />
{/* Modal Footer */}
)}
); } export default SettingModal;