lundi 20 juin 2016

undefined object in angularjs

I am quit new to angularjs, i have created a factory service, which have a authentication service in it. if the authentication succeeded then it generates a token which is required for the dashboard services. i am able to generate the token but not understand how to use it.

Is it a good idea to create a factory service to get token so i should inject in controllers as needed?

Any help highly appreciated. Thanks in advanced.

login.html :

<div ng-app="loginFormApp" ng-controller="loginFormCtrl">

                        <form method="post" action="" id="login_form" class="row">
                            <input type="text" placeholder="Login ID" ng-model="loginId" >
                            <input type="password" placeholder="Password" ng-model="password" >
                            <input type="button" class="btn btn-theme" ng-click="loginCall()" value="Login">
                            <input type="button" class="btn btn-theme" ng-click="loginCall()" value="Register Here">
                        </form>
                    </div>

my controller and factory service :(authService.js)

var app = angular.module('loginFormApp', []);
 app.controller('loginFormCtrl', function ($scope, AuthService) {
     $scope.loginCall = function () {
         var token= AuthService.authentication($scope.loginId, $scope.password);
         alert(token);

     };
 });

 app.factory('AuthService', function ($http) {
     return {
         authentication: function (UserName, Password) {
             $http.post("http://103.19.89.152:8080/ccp-services/authenticate", {
                     'userName': UserName,
                     'password': Password
                 })
                 .then(function (response) {
                         window.location.href = "http://192.168.1.144:2000/angular/dashboard.html";
                         var getToken = response.data.httpHeaders.h5cAuthToken;
                      //   alert(token);

                     },
                     // Error Handling
                     function (response) {
                         console.log(response.datas);
                     });
         }

     }
 });

Aucun commentaire:

Enregistrer un commentaire