From f544ef3c60258a7912c5bd16c5afb4ced049dd62 Mon Sep 17 00:00:00 2001 From: ISA Date: Mon, 24 Feb 2025 08:52:12 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=201=EF=B8=8F=E2=83=A3=20Was=20wurd?= =?UTF-8?q?e=20erfolgreich=20ausgelagert=3F=20=E2=9C=94=20cryptoUtils.ts?= =?UTF-8?q?=20=E2=86=92=20Enth=C3=A4lt=20generateKeyAndIV=20und=20generate?= =?UTF-8?q?Token=E2=80=8BcryptoUtils=20=E2=9C=94=20useAdminAuth.ts=20?= =?UTF-8?q?=E2=86=92=20Enth=C3=A4lt=20decryptToken,=20useEffect=20f=C3=BCr?= =?UTF-8?q?=20Login-Status=20und=20logoutAdmin=E2=80=8BuseAdminAuth=20?= =?UTF-8?q?=E2=9C=94=20useSystemSettings.ts=20=E2=86=92=20Verwaltet=20Redu?= =?UTF-8?q?x-Daten=20und=20setzt=20formValues=E2=80=8BuseSystemSettings=20?= =?UTF-8?q?=E2=9C=94=20handleAdminLogin.ts=20=E2=86=92=20Enth=C3=A4lt=20ha?= =?UTF-8?q?ndleAdminLogin,=20um=20die=20Login-Logik=20auszulagern=E2=80=8B?= =?UTF-8?q?handleAdminLogin=20=E2=9C=94=20handleClearDatabase.ts=20?= =?UTF-8?q?=E2=86=92=20Handhabt=20das=20L=C3=B6schen=20der=20Datenbank?= =?UTF-8?q?=E2=80=8BhandleClearDatabase=20=E2=9C=94=20handleReboot.ts=20?= =?UTF-8?q?=E2=86=92=20Handhabt=20den=20Reboot=20des=20CPL=E2=80=8BhandleR?= =?UTF-8?q?eboot=20=E2=9C=94=20handleSetDateTime.ts=20=E2=86=92=20Setzt=20?= =?UTF-8?q?Datum=20und=20Uhrzeit=20f=C3=BCr=20das=20System=E2=80=8BhandleS?= =?UTF-8?q?etDateTime=20=E2=9C=94=20handleSubmit.ts=20=E2=86=92=20Verarbei?= =?UTF-8?q?tet=20=C3=84nderungen=20und=20sendet=20sie=20an=20den=20Server?= =?UTF-8?q?=E2=80=8BhandleSubmit=20=E2=9C=94=20users.ts=20=E2=86=92=20Enth?= =?UTF-8?q?=C3=A4lt=20die=20USERS-Konfiguration=20f=C3=BCr=20den=20Admin?= =?UTF-8?q?=E2=80=8Busers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🚀 Das bedeutet: SettingsModal.tsx ist jetzt nur noch für die UI zuständig. Alle Geschäftslogik (State, API-Aufrufe, Funktionen) wurde sauber in separate Dateien ausgelagert. Der Code ist jetzt wartungsfreundlicher und wiederverwendbarer. --- .../header/settingsModal/SettingsModal.tsx | 36 +++---------------- .../header/settingsModal/config/users.ts | 10 ++++++ .../handlers/handleAdminLogin.ts | 27 ++++++++++++++ .../settingsModal/hooks/useAdminAuth.ts | 13 ++++--- config/webVersion.ts | 2 +- 5 files changed, 51 insertions(+), 37 deletions(-) create mode 100644 components/header/settingsModal/config/users.ts create mode 100644 components/header/settingsModal/handlers/handleAdminLogin.ts diff --git a/components/header/settingsModal/SettingsModal.tsx b/components/header/settingsModal/SettingsModal.tsx index 1eff96c..deb07f6 100644 --- a/components/header/settingsModal/SettingsModal.tsx +++ b/components/header/settingsModal/SettingsModal.tsx @@ -13,20 +13,14 @@ import CryptoJS from "crypto-js"; import { useAdminAuth } from "./hooks/useAdminAuth"; import { useSystemSettings } from "./hooks/useSystemSettings"; import { generateKeyAndIV, generateToken } from "./utils/cryptoUtils"; +import USERS from "./config/users"; +import handleAdminLogin from "./handlers/handleAdminLogin"; ReactModal.setAppElement("#__next"); -const USERS = { - Admin: { - username: "admin", - // Gehashte Version von "admin" mit bcrypt - password: "$2a$10$xpq/.tcOJN/LXfzdCcCVrenlBh2nRlM1R1ISY7dd1q2qGWC9Fyd2G", - role: "Admin", - }, -}; - function SettingModal({ showModal, onClose }) { - const { isAdminLoggedIn, setAdminLoggedIn } = useAdminAuth(showModal); + const { isAdminLoggedIn, logoutAdmin } = useAdminAuth(showModal); + const { formValues, setFormValues } = useSystemSettings(showModal); const [username, setUsername] = useState(""); @@ -34,24 +28,6 @@ function SettingModal({ showModal, onClose }) { const [error, setError] = useState(""); const [showLoginForm, setShowLoginForm] = useState(false); - function handleAdminLogin(e) { - e.preventDefault(); - const user = USERS.Admin; - bcrypt.compare(password, user.password, (err, isMatch) => { - if (isMatch) { - const token = generateToken(user); - sessionStorage.setItem("token", token); - localStorage.setItem("isAdminLoggedIn", "true"); - setShowLoginForm(false); - onClose(); - } else { - setError( - "Login fehlgeschlagen. Bitte überprüfen Sie Benutzername und Passwort." - ); - } - }); - } - const deviceName_Redux = useSelector( (state: RootState) => state.systemSettings.deviceName ); @@ -108,10 +84,6 @@ function SettingModal({ showModal, onClose }) { ntpTimezone, active, }; - const handleAdminLogout = () => { - sessionStorage.removeItem("token"); // Token aus sessionStorage entfernen - localStorage.setItem("isAdminLoggedIn", "false"); // Admin-Status im localStorage setzen - }; return ( <> diff --git a/components/header/settingsModal/config/users.ts b/components/header/settingsModal/config/users.ts new file mode 100644 index 0000000..b4d7c43 --- /dev/null +++ b/components/header/settingsModal/config/users.ts @@ -0,0 +1,10 @@ +// components/header/settingsModal/config/users.ts +const USERS = { + Admin: { + username: "admin", + password: "$2a$10$xpq/.tcOJN/LXfzdCcCVrenlBh2nRlM1R1ISY7dd1q2qGWC9Fyd2G", // Gehashte Version von "admin" + role: "Admin", + }, +}; + +export default USERS; diff --git a/components/header/settingsModal/handlers/handleAdminLogin.ts b/components/header/settingsModal/handlers/handleAdminLogin.ts new file mode 100644 index 0000000..15bdb11 --- /dev/null +++ b/components/header/settingsModal/handlers/handleAdminLogin.ts @@ -0,0 +1,27 @@ +// components/header/settingsModal/handlers/handleAdminLogin.ts +import bcrypt from "bcryptjs"; +import { generateToken } from "../utils/cryptoUtils"; +import USERS from "../config/users"; + +const handleAdminLogin = ( + username: string, + password: string, + onSuccess: () => void, + onError: (errorMsg: string) => void +) => { + const user = USERS.Admin; + bcrypt.compare(password, user.password, (err, isMatch) => { + if (isMatch) { + const token = generateToken(user); + sessionStorage.setItem("token", token); + localStorage.setItem("isAdminLoggedIn", "true"); + onSuccess(); + } else { + onError( + "Login fehlgeschlagen. Bitte überprüfen Sie Benutzername und Passwort." + ); + } + }); +}; + +export default handleAdminLogin; diff --git a/components/header/settingsModal/hooks/useAdminAuth.ts b/components/header/settingsModal/hooks/useAdminAuth.ts index 6b3f9de..0b13f22 100644 --- a/components/header/settingsModal/hooks/useAdminAuth.ts +++ b/components/header/settingsModal/hooks/useAdminAuth.ts @@ -20,6 +20,12 @@ function decryptToken(encryptedToken: string) { export function useAdminAuth(showModal: boolean) { const [isAdminLoggedIn, setAdminLoggedIn] = useState(false); + function logoutAdmin() { + sessionStorage.removeItem("token"); + localStorage.setItem("isAdminLoggedIn", "false"); + setAdminLoggedIn(false); + } + useEffect(() => { if (showModal) { const token = sessionStorage.getItem("token"); @@ -29,16 +35,15 @@ export function useAdminAuth(showModal: boolean) { if (Date.now() < exp) { setAdminLoggedIn(true); } else { - sessionStorage.removeItem("token"); - setAdminLoggedIn(false); + logoutAdmin(); } } catch (error) { console.error("Token-Entschlüsselung fehlgeschlagen:", error); - setAdminLoggedIn(false); + logoutAdmin(); } } } }, [showModal]); - return { isAdminLoggedIn, setAdminLoggedIn }; + return { isAdminLoggedIn, logoutAdmin }; } diff --git a/config/webVersion.ts b/config/webVersion.ts index a5ffa0e..abafa5f 100644 --- a/config/webVersion.ts +++ b/config/webVersion.ts @@ -6,5 +6,5 @@ 2: Patch oder Hotfix (Bugfixes oder kleine Änderungen). */ -const webVersion = "1.6.97"; +const webVersion = "1.6.98"; export default webVersion;