页面刷新时,Satellizer 登录 JWT 令牌消失了

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

问题描述

我正在使用

.controller('loginCtrl', function($scope, $state, $auth, jwtHelper) {$scope.login = 函数(){$auth.login($scope.user).then(函数(响应){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');})};})

解决方案

你应该试试这个:

<块引用>

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

<块引用>

$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 令牌消失了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一条:Node.js - Express.js JWT 总是在浏览器响应中返回一个无效的令牌错误 下一条:尝试在节点中实现 JWT 身份验证.在受保护的路线中获得未经授权

相关文章

最新文章