我做的是什麼,只要令牌存儲在localStorage的或更好的節省頭JSON如下
if (response.data.token) // response for successful login
{
let headers = {
ContentType : 'application/json',
access-token : response.data.token,
anyOtherHeader: 'xxx'
}
window.localStorage.setItem('headers',JSON.stringify(headers));
}
,只是檢查這個每當應用程序加載,如果它存在,如果它保持的方式它在$ rootScope
let headers = window.localStorage.getItem('headers');
if(headers){
$rootScope.headers = JSON.parse(headers);
$timeout(function(){$scope.$apply()}) //to update your app about token
}
,每當您的令牌不被認證/未驗證/註銷從localStorage的刪除它和$ rootScope
window.localStorage.removeItem('headers')
$rootScope.headers = undefined;
$timeout(function(){$scope.$apply()}); //again to update your app about it
通過適當的錯誤處理,它對我很好。雖然它可以鍛鍊,但不能被濫用。讓我知道如果這有助於:)
你能分享你的代碼 – Codesingh