mardi 16 avril 2019

I need a way to break this loop or an alternative way to do this

I'm new to js and firebase. I'm trying to use firebase database for a custom login by using my own table called users. I have used a for each loop to go through the data. But the else part is executed multiple time because of this. I need to break the loop so it wont happen.

this is my data:-

{"users" : [ { "userId" : "1", "username" : "admin", "password" : "admin", "type" : "admin" }, { "userId" : "2", "username" : "cashier", "password" : "cashier", "type" : "cashier"
}] }**

this is the code i wrote:

var database=firebase.database();

function SignIn(){
var txtuser=document.getElementById('username').value;
var txtpass=document.getElementById('password').value;
var error=false;

firebase.database().ref('users').orderByKey().once('value').then(function(snapshot) {
  snapshot.forEach(function(childSnapshot) {
    var users = childSnapshot.child('username').val();
    var pass=childSnapshot.child('password').val();

    if(txtuser==users && txtpass==pass){
      var type=childSnapshot.child('type').val();       
      if(type=="admin"){
        location.href="admin.html";
      }
      else if(type=="cashier"){
        location.href="cashier.html";
      }            
    }
    else{
      error=true;            
    }       
  });
});

if(error==true)
{
  window.alert("Invalid Credentials");
  location.href="index.html";
}

}




Aucun commentaire:

Enregistrer un commentaire