Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 994
James Swinbank-Slack Avatar asked Aug 14 '26 14:08

James Swinbank-Slack


1 Answers

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';}
  }
};
like image 114
djolf Avatar answered Aug 16 '26 13:08

djolf