javascriptangularjsjwtsatellizer

How to save JWT to localStorage


enter image description here I am using Angular-satellizer extension for the login/register feature but I am stuck at number 7. I cant save JWT to localStorage. I checked the developer tools in chrome but there is no token.

.controller('loginCtrl', function($scope, $state, $auth, jwtHelper, $window) {
$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"}}

    $window.localStorage.setItem('token', JSON.stringify(tokenPayload));

    $state.go('baba.manga');
    })
};
})

Solution

  • You can use both :

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

    or

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

    If it's getting deleted on page refresh then you need to check if you have reset the localStorage somewhere or some script is doing it