页面刷新时,Satellizer登录JWT令牌消失 [英] Satellizer login JWT token is gone when page refresh

查看:195
本文介绍了页面刷新时,Satellizer登录JWT令牌消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用

  .controller('loginCtrl',function($ scope,$ state,$ auth,jwtHelper){
$ scope.login = function(){
$ auth .login($ scope.user)
.then(function(response){
var gelenToken = response.data;
var tokenPa yload = 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');
})
};
})


解决方案

你应该试试这个:


localStorage.setItem('token',data.token);


OR


$ window.localStorage.token = JSON.stringify(data.token);


保存您的令牌,您可以在浏览器本地存储中看到令牌,除非您需要,否则无需解码该令牌。



它不应该在页面刷新时被删除,如果它被删除了,我建议你在地方尝试断点并调试哪个部分正在清除本地存储。



浏览器不会在任何时候删除任何本地存储数据,您的代码可能正在执行此操作。


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.

这篇关于页面刷新时,Satellizer登录JWT令牌消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆