feat: Token in SessionStorage gespeichert und Passwort gehasht
- Admin-Token wird nun in SessionStorage anstelle von LocalStorage gespeichert. - Passwort für Admin-Benutzer ist jetzt mit bcrypt gehasht. - Verbesserte Sicherheit durch die Verwendung von SessionStorage (Daten werden beim Schließen des Tabs gelöscht). - Anpassung von Funktionen zur Token-Verwaltung für SessionStorage.
This commit is contained in:
@@ -11,12 +11,16 @@ import handleSubmit from "./handlers/handleSubmit";
|
|||||||
import { useRouter } from "next/router";
|
import { useRouter } from "next/router";
|
||||||
import { setAdminLoggedIn } from "../../../store/authSlice";
|
import { setAdminLoggedIn } from "../../../store/authSlice";
|
||||||
import { useDispatch } from "react-redux";
|
import { useDispatch } from "react-redux";
|
||||||
|
import bcrypt from "bcryptjs";
|
||||||
|
|
||||||
ReactModal.setAppElement("#__next");
|
ReactModal.setAppElement("#__next");
|
||||||
|
|
||||||
const USERS = {
|
const USERS = {
|
||||||
Admin: { username: "admin", password: "admin", role: "Admin" },
|
Admin: {
|
||||||
Ismail: { username: "ismail", password: "ismail", role: "Admin" },
|
username: "admin",
|
||||||
|
// Gehashte Version von "admin" mit bcrypt
|
||||||
|
password: "$2a$10$xpq/.tcOJN/LXfzdCcCVrenlBh2nRlM1R1ISY7dd1q2qGWC9Fyd2G",
|
||||||
|
role: "Admin",
|
||||||
|
},
|
||||||
};
|
};
|
||||||
// Function to generate JWT token
|
// Function to generate JWT token
|
||||||
function generateToken(user) {
|
function generateToken(user) {
|
||||||
@@ -38,24 +42,23 @@ function SettingModal({ showModal, onClose }) {
|
|||||||
const [showLoginForm, setShowLoginForm] = useState(false); // Zustand für Login-Formular
|
const [showLoginForm, setShowLoginForm] = useState(false); // Zustand für Login-Formular
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
const handleAdminLogin = (e) => {
|
function handleAdminLogin(e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
const user = Object.values(USERS).find(
|
const user = USERS.Admin; // Finde den Admin-Benutzer
|
||||||
(u) => u.username === username && u.password === password
|
bcrypt.compare(password, user.password, (err, isMatch) => {
|
||||||
);
|
if (isMatch) {
|
||||||
|
const token = generateToken(user);
|
||||||
if (user) {
|
sessionStorage.setItem("token", token); // Speichere Token in SessionStorage
|
||||||
const token = generateToken(user);
|
dispatch(setAdminLoggedIn(true));
|
||||||
localStorage.setItem("token", token);
|
setShowLoginForm(false);
|
||||||
dispatch(setAdminLoggedIn(true));
|
onClose();
|
||||||
setShowLoginForm(false);
|
} else {
|
||||||
onClose();
|
setError(
|
||||||
} else {
|
"Login fehlgeschlagen. Bitte überprüfen Sie Benutzername und Passwort."
|
||||||
setError(
|
);
|
||||||
"Login fehlgeschlagen. Bitte überprüfen Sie Benutzername und Passwort."
|
}
|
||||||
);
|
});
|
||||||
}
|
}
|
||||||
};
|
|
||||||
const deviceName_Redux = useSelector((state) => state.variables.deviceName);
|
const deviceName_Redux = useSelector((state) => state.variables.deviceName);
|
||||||
const mac1_Redux = useSelector((state) => state.variables.mac1);
|
const mac1_Redux = useSelector((state) => state.variables.mac1);
|
||||||
const ip_Redux = useSelector((state) => state.variables.ip);
|
const ip_Redux = useSelector((state) => state.variables.ip);
|
||||||
|
|||||||
Reference in New Issue
Block a user