简体   繁体   English

比较两个 arrays 的用户名 + 密码

[英]Comparing two arrays for usernames + passwords

I have two arrays in JavaScript.我在 JavaScript 中有两个 arrays。 One contains usernames and one contains passwords.一个包含用户名,一个包含密码。 I want to create a loop that checks what position (i) the username is in - in the 'approvedUsernames' array - that was inputted by the user, and takes that same 'i' value in the 'approvedPasswords' array and picks the value that was found.我想创建一个循环来检查 position (i) 用户名在用户输入的“approvedUsernames”数组中,并在“approvedPasswords”数组中采用相同的“i”值并选择该值找到了。 then compare the two.然后比较两者。 If they match, a successful login happens, if not it is unsuccessful如果它们匹配,则成功登录,否则不成功

Please see existing Arrays and the code i have already written below请查看现有的 Arrays 和我已经在下面编写的代码

any help greatly appreciated i hope this was clear enough i had trouble wording it:)非常感谢任何帮助我希望这已经足够清楚了我很难措辞:)

James詹姆士

EDIT: I KNOW THIS IS A VERY INSECURE WAY TO STORE PASSWORDS IT IS JUST TEMPORARY TO TEST THE LOGIN ALGORITHM.编辑:我知道这是一种非常不安全的密码存储方式,它只是暂时测试登录算法。 THE FINAL VERSION WILL DEFINITELY BE USING PHP+SQL DATABASE最终版本肯定会使用 PHP+SQL 数据库

Arrays: Arrays:

approvedLogins = ['JamesLiverton', 'SamW']                 approvedPasswords = ['password', 'coding']

Code:代码:

function login(){
var username = document.getElementById('usernameField').value
var password = document.getElementById('passwordField').value

for (i = 0; i < approvedLogins.length; i++) {
    if (username == approvedLogins[i].username && password == approvedPasswords[i].password) {
        alert('Login Sucessful')
        return
    }
    else {
        alert('Login Unsucessful')
        return
    }
}

} }

Check this example:检查这个例子:

var approvedLogins = ['JamesLiverton', 'SamW'];
var approvedPasswords = ['password', 'coding'];

function login(username) {
    if (approvedLogins.includes(username)) {
        var matchedPassword = approvedPasswords[approvedLogins.indexOf(username)];
        console.log(matchedPassword);
    } else {
        console.log("Username not found in array!");
    }
}

It checks if the Username provided in the login() parameter, is found in the array.它检查是否在数组中找到login()参数中提供的用户名。 If it's inside the array, then it gets the password relative to the position of the username within that array.如果它在数组内,那么它会获取与该数组内用户名的 position 相关的密码。 For example, "SamW" would be "coding".例如,“SamW”将是“编码”。

I hope this helps.我希望这有帮助。

First, if you're planning on doing this, I have a feeling that you don't know much about security.首先,如果您打算这样做,我感觉您对安全性知之甚少。 I suggest you look into third party authentication (which, if you're asking this kind of question, might be out of your skill level, but still).我建议您查看第三方身份验证(如果您提出此类问题,可能超出您的技能水平,但仍然如此)。 At the very least, consider encrypting your user's password, with a salt (look up what a salt is).至少,考虑用盐加密您的用户密码(查看什么是盐)。

With that said, you can do this.话虽如此,你可以做到这一点。

function login() {
  const username = document.getElementById('usernameField').value
  const password = document.getElementById('passwordField').value

  alert(isValidLogin(username, password) ? 'Login successful' : 'Login failed')
}

// create a separate function for checking validity, so it's easier
// to refactor/reimplement later, if need be.
function isValidLogin(username, password) {
  const usernameArray = ['name1', 'name2', ... 'nameN']
  const passwordArray = ['pw1', 'pw2', ... 'pwN']

  const usernameIndex = usernameArray.findIndex(item => item === username)
  
  return usernameIndex !== -1 && passwordArray[usernameIndex] === password
}

 let approvedLogins = ['JamesLiverton', 'SamW'] let approvedPasswords = ['password', 'coding'] function login(){ var username = document.getElementById('usernameField').value var password = document.getElementById('passwordField').value let index = approvedLogins.indexOf(username) if (password === approvedPasswords[index]) { alert('Login Sucessful') } else { alert('Login Unsucessful') } }
 <input type="text" id="usernameField" placeholder="username" /><input type="text" id="passwordField" placeholder="password" /> <button onclick="login()">login</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM