Satellite Login JWT Icon Missing on Page Refresh

enter image description here

I am using the extension Angular-satellizerfor the login / registration function, but I am stuck at number 7.

The token was saved in localStorage, but I am refreshing the page and the function $auth.isAuthenticated()returns false.

enter image description here

.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');
      })
  };
})
+4
source share
2 answers

You should try the following:

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

OR

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

to save the token, you can see the token in the local storage of the browser, you do not need to decode this token if you do not need it.

. , ​​, .

, .

+2

, , , .

// if the localScore is not set then initialize it 
if(localStorage.getItem('token') === null) {
     localStorage.setItem('token', JSON.stringify(tokenPayload));
}
+1

Source: https://habr.com/ru/post/1678444/


All Articles