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