問題描述
我正在使用
.controller('loginCtrl', function($scope, $state, $auth, jwtHelper) {$scope.login = 函數(shù)(){$auth.login($scope.user).then(函數(shù)(響應(yīng)){var gelenToken = response.data;var tokenPayload = jwtHelper.decodeToken(gelenToken.token);console.log(JSON.stringify(tokenPayload));//輸出:{"sub":"1","iat":1496346513,"exp":1497556113,"data":{"role":"admin"}}localStorage.setItem('token', JSON.stringify(tokenPayload));$state.go('baba.manga');})};})
你應(yīng)該試試這個(gè):
<塊引用>localStorage.setItem('token', data.token);
或
<塊引用>$window.localStorage.token = JSON.stringify(data.token);
要保存您的令牌,您可以在瀏覽器本地存儲(chǔ)中看到該令牌,除非您需要,否則您無需解碼該令牌.
它不應(yīng)該在頁(yè)面刷新時(shí)被刪除,如果它被刪除了,我建議你在地方嘗試斷點(diǎn)并調(diào)試哪個(gè)部分正在清除本地存儲(chǔ).
瀏覽器在任何時(shí)候都不會(huì)刪除任何本地存儲(chǔ)數(shù)據(jù),您的代碼可能正在這樣做.
I am using the Angular-satellizer
extension for the login/register feature but I am stuck at number 7.
Token was saved to localStorage
but I when refresh the page it's gone and $auth.isAuthenticated()
function is returning false.
.controller('loginCtrl', function($scope, $state, $auth, jwtHelper) {
$scope.login = function() {
$auth.login($scope.user)
.then(function(response) {
var gelenToken = response.data;
var tokenPayload = jwtHelper.decodeToken(gelenToken.token);
console.log(JSON.stringify(tokenPayload)); // Output:{"sub":"1","iat":1496346513,"exp":1497556113,"data":{"role":"admin"}}
localStorage.setItem('token', JSON.stringify(tokenPayload));
$state.go('baba.manga');
})
};
})
You should try this :
localStorage.setItem('token', data.token);
OR
$window.localStorage.token = JSON.stringify(data.token);
to save your token, You can see the token in browser local Storage, You don't need to decode that token unless you need it to.
It should not get deleted on page refresh, If its getting deleted the I suggest you to try breakpoints at places and debug which part is clearing local storage.
Browser does not delete any localstorage data at any point, Your code might be doing it.
這篇關(guān)于頁(yè)面刷新時(shí),Satellizer 登錄 JWT 令牌消失了的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!