1

I am currently trying to create a user login page where the user information is stored on a json file. I have created my GET method to the file but I cannot seem to redirect after the user has logged in successfully. Can you help me out?

I know that user login and validation done on the client side is A BAD IDEA but that is how I want to do it, without using a database.

Here is my HTML code:

<body ng-controller="myCtrl">
    <form id="login-form">
            <h3>User Login</h3>
                   <div class="form-group">
                    <label for="email">Email address:</label>
                    <input type="email" class="form-control" id="email">
                    </div>

                  <div class="form-group">
                    <label for="pwd">Password:</label>
                    <input type="password" class="form-control" id="pwd">
                  </div>
                  <button type="submit" class="btn btn-default" id="login-button" ng-click="LogOn()">Login</button>
        </form>
</body>

My JavaScript code:

var app = angular.module('myApp', [ ]);
                app.controller("myCtrl",['$scope', '$http', function($scope, $http){
                $scope.LogOn = function(){
                    $http.get('data.json').then(function(data){
                    $scope.users = data;
                    });
     if (data.email == email) && (data.password = password{
            window.location.href = 'www.google.com';
          }             
                    };  
                }]);

My JSON File:

[
    {
        "email":"[email protected]",
        "Password":"password"
    },
    {
        "email":"[email protected]",
        "password":"test999"
    },
    {
        "email":"[email protected]",
        "password":"xxx"
    }
]
1
  • You might want to look into ngRoute or ui.router. Commented Mar 22, 2017 at 14:05

3 Answers 3

3

Please try to open the link and check and run the code which is deployed in w3 schools https://www.w3schools.com/code/tryit.asp?filename=FDVZY3NXVYG6

Sign up to request clarification or add additional context in comments.

Comments

0

Put redirection code inside then

$http.get('data.json').then(function(data){
          $scope.users = data;
          if (data.email == email && data.password == password) {
            window.location.href = 'www.google.com';
          }   
});

1 Comment

You're comparing an array of data against two strings. You'll need to loop the users and match every user and password if you want to be able to enter your if stack.
0

Obviously the if bellow has to be inside the .then( callback

if (data.email == email && data.password == password) {
   window.location.href = 'www.google.com';
}

2 Comments

Uncaught ReferenceError: Invalid left-hand side in assignment - if (data.email == email) && (data.password = password)
Yes but checking the code if it runs in the first place is a vital part of giving an actual working answer. Thanks for your effort tho'

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.