git commit -am "fix: Admin-Status im KueEinstellung via useAdminAuth statt Redux"
für z.B. Kue Firmware Update
This commit is contained in:
@@ -16,6 +16,7 @@ import { setKueData } from "../../../../../redux/slices/kueDataSlice";
|
||||
import handleSave, { OriginalValues } from "../handlers/handleSave";
|
||||
import handleDisplayEinschalten from "../handlers/handleDisplayEinschalten";
|
||||
import firmwareUpdate from "../handlers/firmwareUpdate";
|
||||
import { useAdminAuth } from "../../../settingsPageComponents/hooks/useAdminAuth";
|
||||
|
||||
interface Props {
|
||||
slot: number;
|
||||
@@ -38,9 +39,7 @@ export default function KueEinstellung({
|
||||
kueLimit2High,
|
||||
} = useSelector((state: RootState) => state.kueDataSlice);
|
||||
|
||||
const isAdminLoggedIn = useSelector(
|
||||
(state: any) => state.authSlice.isAdminLoggedIn
|
||||
);
|
||||
const { isAdminLoggedIn } = useAdminAuth(true);
|
||||
|
||||
const handleSaveWrapper = () => {
|
||||
const originalValues: OriginalValues = {
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import CryptoJS from "crypto-js";
|
||||
|
||||
/**
|
||||
* Entschlüsselt den gespeicherten Token (AES) aus sessionStorage.
|
||||
*/
|
||||
function decryptToken(encryptedToken: string) {
|
||||
const encryptionKey = process.env.NEXT_PUBLIC_ENCRYPTION_KEY;
|
||||
const encryptionIV = process.env.NEXT_PUBLIC_ENCRYPTION_IV;
|
||||
@@ -13,13 +16,16 @@ function decryptToken(encryptedToken: string) {
|
||||
const key = CryptoJS.enc.Utf8.parse(encryptionKey);
|
||||
const iv = CryptoJS.enc.Utf8.parse(encryptionIV);
|
||||
|
||||
const bytes = CryptoJS.AES.decrypt(encryptedToken, key, { iv });
|
||||
return JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
|
||||
const decrypted = CryptoJS.AES.decrypt(encryptedToken, key, { iv });
|
||||
return JSON.parse(decrypted.toString(CryptoJS.enc.Utf8));
|
||||
}
|
||||
|
||||
export function useAdminAuth(showModal: boolean) {
|
||||
const [isAdminLoggedIn, setAdminLoggedIn] = useState(false);
|
||||
|
||||
/**
|
||||
* Loggt den Admin aus und löscht token + localStorage
|
||||
*/
|
||||
function logoutAdmin() {
|
||||
sessionStorage.removeItem("token");
|
||||
localStorage.setItem("isAdminLoggedIn", "false");
|
||||
@@ -29,19 +35,29 @@ export function useAdminAuth(showModal: boolean) {
|
||||
useEffect(() => {
|
||||
if (showModal) {
|
||||
const token = sessionStorage.getItem("token");
|
||||
|
||||
if (token) {
|
||||
try {
|
||||
const { exp } = decryptToken(token);
|
||||
|
||||
// ✅ Token gültig
|
||||
if (Date.now() < exp) {
|
||||
localStorage.setItem("isAdminLoggedIn", "true");
|
||||
setAdminLoggedIn(true);
|
||||
} else {
|
||||
logoutAdmin();
|
||||
return;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Token-Entschlüsselung fehlgeschlagen:", error);
|
||||
logoutAdmin();
|
||||
console.error("❌ Token-Entschlüsselung fehlgeschlagen:", error);
|
||||
}
|
||||
}
|
||||
|
||||
// 🔁 Fallback: prüfe ob localStorage Adminstatus hält
|
||||
const fromLocalStorage = localStorage.getItem("isAdminLoggedIn");
|
||||
if (fromLocalStorage === "true") {
|
||||
setAdminLoggedIn(true);
|
||||
} else {
|
||||
logoutAdmin();
|
||||
}
|
||||
}
|
||||
}, [showModal]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user