2016-06-17 4 views
0

私はangularjsを終了しました。私はそれに認証サービスを持つファクトリサービスを作成しました。認証が成功すると、ダッシュボードサービスに必要なトークンが生成されます。私はトークンを生成することができますが、それを使用する方法を理解していません。angularjsで定義されていないオブジェクト

は、必要なので、私はコントローラに注入しなければならないトークンを取得するために工場サービスを作成することをお勧めしますか?

ご協力いただきありがとうございます。ありがとうございました。

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> 

私のコントローラと工場出荷時のサービス:(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); 
        }); 
     } 

    } 
}); 
+0

トークンをsessionStorageに格納できます。詳細はこちら[こちら](https://auth0.com/blog/2014/01/07/angularjs-authentication-with-cookies-vs-token/) – Matheno

+0

システムの複数の領域それはトークンを必要とし、すべてがこのサービスに当たって、それぞれが理想的ではない遠い独自の要求をサーバーに出すでしょう。1つのリクエストを作成し、そのレスポンスを何らかの形でキャッシュするだけで、他のすべてがキャッシュされたバージョンを取得する必要があります。受信されるトークンをブロードキャストするための別個の約束またはイベントを可能にする。どのようにトークンを使用していますか?そのヘッダーの中にインターセプターを実装してコントローラなどを扱う必要がない場合(すべてが一度あなたのサービスから約束を返すことになっていれば) – ste2425

答えて

2

$ http.postがa promiseを返すため、このコードは機能しません。

var token = AuthService.authentication($scope.loginId, $scope.password); 

まず、以下に示すように$ http.postメソッドを返す必要があります。 then方法で

return $http.post(// rest of code 

$http.postした後、あなたはトークンを返す必要があります。

.then(function (response) { 
    window.location.href = "http://192.168.1.144:2000/angular/dashboard.html"; 
    return response.data.httpHeaders.h5cAuthToken; //return token 
}, 

そして、あなたのコントローラでのログイン・コールは

AuthService.authentication($scope.loginId, $scope.password).then(function(token) { 
    alert(token); 
}); 

アップデート1でなければなりません:あなたはAPI呼び出しにアクセストークンを再利用できるようにしたい勿論

アクセストークンを再利用認証コールの後にこれは次のようにして行うことができます。呼び出し元のメソッドにアクセストークンを返す代わりに、サービス自体の内部にトークンをキャッシュすることができます。ダッシュボードコントローラで

app.factory('AuthService', function ($http) { 
    var cachedToken; // this is where the token will be saved after authentication 
    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"; 
       cachedToken = response.data.httpHeaders.h5cAuthToken; // save token to 'cache' 
       return cachedToken 
      }, 
      function (response) { // Error Handling 
       console.log(response.datas); 
      }); 
     }, 
     getToken: function() { // new method to retrieve the cached token 
      return cachedToken; 
     } 
    } 
}); 

、あなたがトークンを取得することができます。

AuthService.getToken(); 

勿論、あなたは(それ以外の場合は、あなたがundefinedを取得します)トークンが実際に検索されたかどうかを確認するために、追加のコードが必要です。

+0

このようにしてVadiem Janssensに感謝します。私はloginFormCtrlコントローラのトークンを取得しますが、dashboardControllerのような他のコントローラでAuthServiceサービスを使用してトークンをどのように達成できるのでしょうか。 –

+0

Ashish、私の投稿の更新を確認してください。 –

+0

あなたの返事をありがとう、それは私の仕事です。私はこの更新されたコードでトークンを取得することができます。 –

0

$ http.postは非同期呼び出しますので、あなたは、トークン変数を取得するためにコールバックを使用する必要があります認証関数はそれを返しません。

+1

彼は...チェーンに入っています。 – Matheno

0

以下の工場を使用してください。私は$ httpコールでリターンを追加しました。

app.factory('AuthService', function ($http) { 
return { 
    authentication: function (UserName, Password) { 
     return $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); 
       }); 
    } 

} 

});

+1

あなたは何をしたのか、なぜそれをしたのか説明してください。OPは実際に何かを学びます。 – Matheno

関連する問題