1
我使用JWT進行授權。在AngularJS 1.x中我用下面的函數來登錄:
function login(username, password) {
var deferred = $q.defer();
$http.post("/api/login", {
"username": username,
"password": password
}).then(function(response) {
if (response.data && response.data.token) {
var tokenBegin = response.data.token.substring(0,6);
var bearer = (tokenBegin === 'Bearer') ? '' : 'Bearer ';
$http.defaults.headers.common.Authorization = bearer + response.data.token;
deferred.resolve({ status: true, message: 'Login complete' });
} else {
deferred.reject(response);
}
}, function(reason) {
deferred.reject(reason)
});
return deferred.promise;
}
被修改$http.defaults
對象。由於Http類似乎沒有defaults
對象,所以我在Angular2和TypeScript中有點失落。我會怎麼做?
我認爲是這樣的:merge(options?:RequestOptionsArgs):RequestOptions options.headers.Authorization ='Bearer:sometoken'; return super.merge(options); }' – stwissel