// 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;
