Search code examples
javascriptarraysjsonangularjsserver-side

Use server-side JSON array objects as login details in AngularJS 1.4


I have a server-side JSON array which I can retrieve. However I have issues trying to use the objects of the array as login details.

$scope.loginvalidator = function(){
   var logins = [ 
        { 
        username: $scope.Users.LoginName,
        password: $scope.Users.Password,
        },
         { 
           username: '1',
           password: '1',
         },
          ];
 for(var i = 0; i<logins.length; i++) {
    if ($scope.userInput == logins[i].username &&
        $scope.pswInput == logins[i].password){
          $scope.feedback = 'Login Successful';
          return true;
    }
    else {$scope.feedback = 'Login Failed';}
  }
  };

My code is only recognizing the hardcoded words and not reading $scope.Users.LoginName/Password as the LoginName and Password kept in the JSON array. I have bound the file read to an ng-click which I run before trying to login.


Solution

  • It looks like the for loop is not stopped. Instead of 'return true', try 'break;'

    for(var i = 0; i<logins.length; i++) {
      if ($scope.userInput == logins[i].username &&
        $scope.pswInput == logins[i].password){
          $scope.feedback = 'Login Successful';
          break; //change this line
        }
        else {$scope.feedback = 'Login Failed';}
      }
    };