Search code examples
javascriptcode.org

confusing bug with login system in code.org


I am making a little app using code.org, however, my app requires for a login. When creating a login, I want it to check to make sure that the user's input does not exist inside the database. I don't want multiple people with the same username. When I had just one account in the database, it worked just fine. But when there is more than one, it no longer does it's job correctly. It DOES check to see if the user's input is equal to what usernames are in the database, and it does tell the user that it is a invalid username, but then it sends them to the next screen anyways and saves the user's inputted username and passcode into the database.

Here is what I have down (I use my own function for alert because code.org doesn't allow the alert function to be used.)

// Alert function
function Alert(text,time) {
  if(text != undefined) {
    textLabel("ALERT",text);
    setProperty("ALERT","background-color","black");
    setProperty("ALERT","text-color","white");
    if(time != undefined) {
      setTimeout(function() {
        deleteElement("ALERT");
      },time);
    }
  }else{
    console.log("WARNING: \"Alert()\" -> \"text\" is undefined.");
  }
}

// create account button
onEvent("create-account-bt","click",function() {
  readRecords("players",{},function(record) {
    for(var i = 0 ; i < record.length ; i++) {
      if(getText("new-username-input") != record[i].username) {
        if(getText("new-passcode-input") != "") {
          createRecord("players",{
            username:getText("new-username-input"),
            passcode:getText("new-passcode-input")
          },function() {
            setScreen("login-screen");
          });  
        }else{
          Alert("Incorrect passcode or username",1200);
        }
      }else{
        Alert("Incorrect passcode or username",1200);
      }
    }
  });
});

I have been trying to figure this out for a long time. I would really appreciate any help, even if it didn't completely fix it.


Solution

  • Your approach is wrong. When you want to make sure that there is no duplicate, you have to run through the entire database. If you find a match while browsing, alert and stop. But only if you could not find a match at the end, add the entry.

    Your current code adds the new entry as soon as there is no match. So almost always directly with the first entry. And even if it finds a match with the first entry, it just alerts but continues the loop as you don't call break.

    Try it this way:

    onEvent("create-account-bt","click",function() {
      readRecords("players",{},function(record) {
    
        var matchFound = false;
    
        for(var i = 0 ; i < record.length ; i++) {
    
          if((getText("new-username-input") == record[i].username) || 
             (getText("new-passcode-input") == "")) 
          {
            Alert("Incorrect passcode or username",1200);
            matchFound = true;
            break;
          }
    
        }
    
        if(!matchFound) {
            createRecord("players",{
                username:getText("new-username-input"),
                passcode:getText("new-passcode-input")
            },function() {
                setScreen("login-screen");
            });    
        }
    
      });
    });