3
私は学生で、最近はangularJSインターセプターに取り組み、セッション管理を制御するために開発しようとしています。私は平均的なスタック開発で新しく、助けが必要です。 誰かがangularJSによるセッション管理の実例を持っていますか?どのように制御するためにangularjsインターセプターを開発する
本当にありがとうございます。
私は学生で、最近はangularJSインターセプターに取り組み、セッション管理を制御するために開発しようとしています。私は平均的なスタック開発で新しく、助けが必要です。 誰かがangularJSによるセッション管理の実例を持っていますか?どのように制御するためにangularjsインターセプターを開発する
本当にありがとうございます。
あなたはこのような何かを行うことができ、トークンベースのコントロールしたい場合:
をあなたのインターセプタ:あなたのよう
angular.module('yourApp', []).config(function($httpProvider) {
$httpProvider.interceptors.push('YourHttpInterceptor');
}
:
angular.module('yourApp').factory('YourHttpInterceptor', ['$q', '$window',
function($q, $window) {
return {
'request': function(config) {
config.headers = config.headers || {};
// If you have a token in local storage for example:
if ($window.localStorage.token) {
// Add the token to "Authorization" header in every request
config.headers.Authorization = 'Bearer ' + $window.localStorage.token;
// In your server you can check if the token is valid and if it's not,
// in responseError method you can take some action
}
// Handle something else
return config;
},
// Optional method
'requestError': function(rejection) {
// do something on request error
if (canRecover(rejection)) {
return responseOrNewPromise
}
return $q.reject(rejection);
},
// Optional method
'response': function(response) {
// do something on response success
return response;
},
// optional method
'responseError': function(rejection) {
// Here you can do something in response error, like handle errors, present error messages etc.
if(rejection.status === 401) { // Unauthorized
// do something
}
if (canRecover(rejection)) {
return responseOrNewPromise
}
return $q.reject(rejection);
}
};
}]);
そして、あなたのモジュールのconfigにインターセプタを登録しますthis postでトークンベースの認証がこの手順(ほとんどの場合)に従います: