我做的是什么,只要令牌存储在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