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

Satellizer login JWT token is gone when page refresh(页面刷新时,Satellizer 登录 JWT 令牌消失了)
本文介绍了页面刷新时,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 令牌消失了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Using discord.js to detect image and respond(使用 discord.js 检测图像并响应)
Check if user ID exists in Discord server(检查 Discord 服务器中是否存在用户 ID)
Guild Member Add does not work (discordjs)(公会成员添加不起作用(discordjs))
Creating my first bot using REPLIT but always error Discord.JS(使用 REPLIT 创建我的第一个机器人,但总是错误 Discord.JS)
How do I code event/command handlers for my Discord.js bot?(如何为我的 Discord.js 机器人编写事件/命令处理程序?)
How to find a User ID from a Username in Discord.js?(如何从 Discord.js 中的用户名中查找用户 ID?)