SRK
SRK

Reputation: 163

authentication problems while using json data in AngularJS

I am new to Angularjs and I am facing an issue while parsing a JSON response. I have created login page and for accessing data I have used JSON File. I am getting error when calling factory function.

HTML:

<form ng-submit="loginform(logcred)" class="ng-scope ng-pristine ng-valid center" name="logform"><br/><br>
<div>
  <label form="emailinput"><b>Email</b></label>
  <input type="email" class="form-control" name="uname" id="emailinput" placeholder="[email protected]" ng-model="logcred.username" >
</div>

<div>
  <label form="pwdinput"><b>Password</b></label>
  <input type="password" class="form-control" name="pwd" id="pwdinput" placeholder="*******" ng-model="logcred.password">
</div>

<div>
    <button type="cancel" class="btn" ng-click="toggle_cancel()">Cancel</button>
    <button class="btn btn-primary" ng-click="submit()">Login</button>
</div>
<br/>   
</form>

AngularJS :

app.factory('auth',function($http,session) {
var authService = {};

authService.login = function(userCred) {
$http.post('http://localhost:3000/loginfo',userCred)
.then(
  function successCallback(response){
  console.log(response);
  session.create(response.data.id);
  var user = response.data;
 if (user [user.username && user.password]) {
 var userCred = user[user.username && user.password];
 if (userCred.username == user.username && userCred.password == user.password) {
    $scope.signinfo = response.data;
} else {
    console.log('Error');
  }
 }
});
};

authService.isAuthenticated = function() {
 return {
 isAuthenticated : false,
 user : null
 }
};

return authService;
});         

var app = angular.module('logpp', []);
  app.controller('credientials', function($scope, $http,auth) {
$scope.logForm = {};

$scope.userCred = {
    username : '',
    password : ''
};

    /*-----Form Submition-----*/

$scope.log = function(){
    auth.isAuthenticated = true;
    if (!$scope.logForm.$invalid) {
        $scope.login($scope.userCred);
    } else {
        console.log('error');
        return;
    }
};

    /*-----Calling Factory Function-----*/

$scope.login = function(userCred) {
    auth.isAuthenticated = true;
    auth.login(function(user) {
        console.log('success');
    },function(error) {
        console.log("error");
    });
}   

when I enter my username and password and click on login()btn , I am getting Possibly unhandled rejection in my console, I don't know why. I re-checked my code , when I submit the form , the error is getting generated from the factory function , I checked it in the console. But I am not understanding what mistake I am doing ?

Any help / advice would be greatly appreciated.

Upvotes: 1

Views: 74

Answers (1)

Rakesh Burbure
Rakesh Burbure

Reputation: 1045

Buddy, you have declared the main app variable as myApp, and attaching factory to app variable.

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


app.factory('auth',function($http,session) {
    var authService = {};

Updated answer

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


myApp.factory('auth',function($http,session) {
    var authService = {};

    authService.login = function(userCred) {
        $http.post('http://localhost:3000/loginfo',userCred)
        .then(
          function successCallback(response){
              console.log(response);
              session.create(response.data.id);
              var user = response.data;
              if (user [user.username && user.password]) {
                  var userCred = user[user.username && user.password];
                  if (userCred.username == user.username && userCred.password == user.password) {
                      $scope.signinfo = response.data;
                  } else {
                      console.log('Error');
                  }
              }
          });
    };

    authService.isAuthenticated = function() {
        return {
            isAuthenticated : false,
            user : null
        }
    };

    return authService;
});         


myApp.controller('credientials', function ($scope, $http, auth) {
    $scope.logForm = {};

    $scope.userCred = {
        username: '',
        password: ''
    };

    /*-----Form Submition-----*/

    $scope.log = function () {
        auth.isAuthenticated = true;
        if (!$scope.logForm.$invalid) {
            $scope.login($scope.userCred);
        } else {
            console.log('error');
            return;
        }
    };

    /*-----Calling Factory Function-----*/

    $scope.login = function (userCred) {
        auth.isAuthenticated = true;
        auth.login(function (user) {
            console.log('success');
        }, function (error) {
            console.log("error");
        }
        );
    }
});

Upvotes: 1

Related Questions