首页 > 解决方案 > “简单”发布登录

问题描述

我试图找出一个基于角色的登录系统,使用 JavaScript 将用户重定向到两个不同的 HTML 页面。我使用 localstorage 来存储密码、用户名和 AuthenticationLevel(这应该是用户级别 1 用户和级别 2 用户之间的区别)。

  1. 制作了一个 UserLogin 类并存储在 LocalStorage 中
  2. 尝试制作一个简单的登录系统,从我的本地存储中提取用户名、密码和身份验证级别,以将用户重定向到两个不同的站点。

但我明白了

未捕获的 SyntaxError:标识符“UserLogin”已被声明为 User.js:25

//Users
class UserLogin {
  constructor(username, password, authLevel) {
    this.username = username;
    this.password = password;
    this.authlevel = authLevel;
  }
}
// Localstorage logins
if (localStorage.getItem("userLogin") == null) {
  var userLogins = [];
  userLogins.push(new UserLogin("Benjamin", 4321, "1"));
  userLogins.push(new UserLogin("Mads", 12345, "1"));
  userLogins.push(new UserLogin("Simon", 1234, "1"));
  userLogins.push(new UserLogin("Jessica", 54321, "1"));
  // Logins for Projectmanagers
  userLogins.push(new UserLogin("Oliver", 1234, "2"));
  userLogins.push(new UserLogin("Sara", 4321, "2"));

  var userLoginstring = JSON.stringify(userLogins)
  localStorage.setItem("userLogin", userLoginstring)
} else {

}
var UserLogin = JSON.parse(localStorage.getItem("UserLogin"))


this.usernameInput = document.getElementById("username");
this.passwordInput = document.getElementById("password");

let aLevel = JSON.parse(localStorage.getItem("authLevel"));

let existingUser = JSON.parse(localStorage.getItem("UserLogin"));

function checkLogin() {

  for (let i = 0; i < existingUser.length; i++) {
    if (usernameInput.value == existingUser[i].employee_user && passwordInput.value == existingUser[i].password) {
      currentLogin.push({
        username: usernameInput
      })
      var IDString = JSON.stringify(currentLogin);
      localStorage.setItem("UserLogin", IDString);
      alert("Login successfully");
      console.log('virker');
      if (aLevel[i].level == 2) {
        document.location = "employeeList.html";

      } else {
        document.location = "Medarbejderside.html";
      }
      return true;
    } else {
      console.log('Doesn'
        t work ');
        attempt--; // Decrementing by one.
        alert("You have wrong attempt;");
        // Disabling fields after 3 attempts.
        if (attempt === 0) {
          document.getElementById("username").disabled = true;
          document.getElementById("password").disabled = true;
          document.getElementById("submit").disabled = true;
          return false;
        }
      }
    }
  }

标签: javascripthtml

解决方案


推荐阅读