0

I was trying to implement login form by authenticating the credentials from data stored in json file. But i'm getting error like only first case is working.It's just a demo application trying to learn the concepts: this is my controller:

var app = angular.module('myApp', []);

app.controller("myCtrl",function($scope, $http)
 {  
      $scope.check = function(){
           var sample;
                     $http.get('roles.json').then(function(res)
                     {
                        sample = res.data;

                    console.log(sample);

                    angular.forEach(sample, function(val)
                    {
                       if($scope.uName===val.userName)
                       {
                           if($scope.password===val.password)
                           {
                               alert("sucess");
                           }
                           else
                           {
                               alert("failure");
                           }
                       }
                       else
                       {
                           alert("failure");
                       }

                  });
              }); // end of http
       };// end of function   
 });

data is loading properly but seems like some problem in logic. data in json:

[
  {"userName":"stud101","password":"stud1","role":"student"},
  {"userName":"stud102","password":"stud2","role":"student"},
  {"userName":"superlib","password":"lib1","role":"Librarian"}
  ]

I'm getting success only with first case, in rest other cases failure.

3
  • What does your HTML look like? What is the exact Error message? Commented Oct 1, 2016 at 17:59
  • There is no point having a login if you are going to send passwords and usernames to the browser. Anyone can see them. With that being said...show us what the data looks like so we can help make what you do have work Commented Oct 1, 2016 at 18:01
  • your condition is checking both uName and password is equal to val or not , Its seems bit odd .Please check that Commented Oct 1, 2016 at 18:06

2 Answers 2

1
$http.get('roles.json').then(function(res){
   sample = res.data;

   console.log(sample);
   var isMatched = false;
   angular.forEach(sample, function(val)
   {
     if($scope.uName==val.userName && $scope.password==val.password)
     {
        isMatched = true;
         return false; // To stop the foreach loop if username and password both are matched.
     }


   });
   if(isMatched)
   {
    alert("success");
   }
else
{
    alert("failure");
}
});
Sign up to request clarification or add additional context in comments.

Comments

0
angular.forEach($scope.messagepool, function(value, key) {
  if(value.userName==$scope.uName  && value.password==$scope.password){
    alert('success');
  }else{
    alert('faluire');
  } 
});

Use this instead of your foreach . Hope this helps (y)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.