Recurssion for login?

Is recurssion an ok way to check if an entered user/pass is correct?

  • I have an array w/ subarrays of username: and password:
  • If username / pw !== the info stored in userAccounts[n] it loops round userAccounts.length[n-1] until it gets a match, or reaches -1 and returns “incorrect username passsword”

It seems like a pretty silly way of doing things, considering it cycles every single username/password we have and logs so in the console “NaN” for every failed attempt

const userAccounts = [
    {
        username: "sam",
        password: "password",
        email: "sam@gmail.com"
    },
    {
        username: "paul",
        password: "paulpass",
        email: "paul@hotmail.co.uk"
    }
]


function getInfo() {

    let userAttempt = document.getElementById("username").value
    let passAttempt = document.getElementById("password").value
    let n = userAccounts.length

    if (userAttempt !== "" && passAttempt !== "") {
        loginLoop(userAttempt, passAttempt);

        function loginLoop(userTry, passTry) {
            if (n > 0) {
                if (userTry !== userAccounts[n - 1].username || passTry !== userAccounts[n - 1].password) {
                    console.log("not authenticated " + n - 1)
                    n--;
                    loginLoop(userAttempt, passAttempt);
                } else {
                    return console.log("login authenticated")
                }

            } else {
                return console.log("incorrect username password")
            }
        }


    } else {
        return console.log("please enter a username and password")
    }

}




I think normally a hashing function is used for this type of thing. (Such that each userid is hashed to a specific location and if you can’t find a match there, then you would stop looking)

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.