1
私は角のテンプレートを持っています。私は、この設定を持っているapp.jsで:角度の認証インターセプタとは何ですか
app.config(function ($routeProvider, $httpProvider) {
$httpProvider.interceptors.push('AuthInterceptor');
$routeProvider
.when('/', { .....etc
AuthInterceptorは、この工場である:
'use strict';
app.factory('AuthInterceptor', function ($rootScope, $q, $window, $location) {
return {
request: function (config) {
config.headers = config.headers || {};
if ($window.localStorage.token) {
config.headers.Authorization = 'Token ' + $window.localStorage.token;
}
return config;
},
responseError: function (response) {
if (response.status === 401) {
$window.localStorage.removeItem('token');
$window.localStorage.removeItem('email');
$location.path('/');
return;
}
return $q.reject(response);
}
};
});
それが正確に何をしますか?
トークンを含むAuthorizationヘッダーを追加します。トークンはlocalStorageに格納され、サーバーが401を返すと削除されます。 – JEY