javascript - “简单”发布登录
问题描述
我试图找出一个基于角色的登录系统,使用 JavaScript 将用户重定向到两个不同的 HTML 页面。我使用 localstorage 来存储密码、用户名和 AuthenticationLevel(这应该是用户级别 1 用户和级别 2 用户之间的区别)。
- 制作了一个 UserLogin 类并存储在 LocalStorage 中
- 尝试制作一个简单的登录系统,从我的本地存储中提取用户名、密码和身份验证级别,以将用户重定向到两个不同的站点。
但我明白了
未捕获的 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;
}
}
}
}
解决方案
推荐阅读
- node.js - Sequelize 将类方法移动到单个文件
- javascript - JSDoc:在另一个@param 中引用方法的@param
- angular - Angular 6 - 使用 Chrome 缓慢打开 Mat-Dialog
- c++ - 试图编译 googletest 但出现以下错误:'::OpenThread' 尚未声明
- ios - 快速在右栏按钮项中添加徽章警报
- c# - MySQL C# 错误;“连接必须有效且打开”
- c - vxWorks 6.9下的shell会话和调试printf
- c - 如何在 Linux 上为 gcc 制作一个简单的 makefile
- java - 获取窗口状态
- python - 使用 3 空间坐标的 3 空间对象的体积