3

I'm working with angular.js and ngstore. I'm using a token based authentication with a node.js REST service, from the angular app I send my credentials to rest server and I receive the token, I then store this token in $localStorage to use throughout all the angular app($localStorage.token). But it turns that sometimes $localStorage.token is undefined, even when I assigned the token to it, so when I call another rest endpoint sending the token in the headers, I'm actually sending an undefined value. Also when I try to logout I do

delete $localStorage.token

but when I check if the user has been loggedout actually the token still there. What is strange is that if I set breakpoints right after deleting the token or assigning the token and wait for a while, everything works, that's making me think that those operations may be asynchronous?

Is this a common problem? How could I fix this?

Any help would appreciated, Thanks.

EDIT: actually I found that the problem is when using window.location, if I use $location.path it's working, but for certain reasons I need to use window.location, and it should work as far as I know

Jorge
  • 33
  • 1
  • 4

2 Answers2

5

I had the same problem today, and using the following commit worked for me.

https://github.com/raynode/ngStorage

There is a helpful discussion about this problem here:

https://github.com/gsklee/ngStorage/issues/39

In this version of ngStorage, the author has thoughtfully provided a way to "save" before proceeding and performing a $window.location.reload();

During login:

$localStorage.value = 100;
$localStorage.$save();

(and then) $window.location.reload();

During logout:

delete $localStorage.value;
$localStorage.$save();
$window.location.reload();

This worked for me to ensure that the $localStorage variables were deleted before page reload.

claireablani
  • 7,804
  • 5
  • 16
  • 19
  • I think this is exactly what I need, I tried to use it but I can't find this package in bower, how did you do to install it? if I do 'bower install ngstorage' it installs the original version and not the fork. thanks – Jorge Feb 26 '15 at 19:24
  • 1
    Hi @Jorge, I actually didn't use bower to install it. I manually installed it by downloading the zip file from https://github.com/raynode/ngStorage. I included a script link to ngStorage.min.js in my index.html file and added ngStorage as a dependency, and it worked fine. Hope that helps! – claireablani Feb 26 '15 at 23:19
  • Holy Cow. I was getting expected behavior in IE9 and IE11 and Firefox. But IE10 and Chrome were not "disposing" of the items in $sessionStorage (even though a simple "alert" showed them as undefined after running $sessionStorage.$reset(); I thought I was losing it. Thanks for this post. – granadaCoder Aug 27 '15 at 20:34
  • No need to install that ngStorage fork. **$localStorage.$apply();** did the trick for me. thanks to this [github issue comment](http://github.com/gsklee/ngStorage/issues/124) – Pavel Bely Mar 09 '17 at 14:50
1

No, local storage is not asynchronous.

JavaScript is a single thread environment and read/write operations to local storage occurs immediately.

Both session and local storage containers are the same. Except that session storage is a key/value pair with an expire timestamp specified.

Storage in HTML5 is not a reliable resource. As there are a number of browser states that will restrict or remove storage.

You are referring to ngStore which is a module I've never heard of. There is no local storage module that is included with AngularJS by default, and there are multiple open source modules that handle this.

What is most likely happening is that you are handling a session token as a state variable instead of a state promise. When you use a variable to hold the state of a resource on the server, then that state has 3 possible values. 1 not assigned, 2 pending assignment and 3 value assigned.

Instead of reading the token from storage directly. You should have a service that returns a promise that will resolve to provide that token. This promise will only resolve after the REST operation has completed.

Reactgular
  • 52,335
  • 19
  • 158
  • 208
  • 1
    the angular library name is ngstorage sorry, I've seen an example almost like what I'm doing and it's working, it's something I am doing wrong. I have discovered that if after loging in I use window.location = "/" to redirect to home page the localStorage object doesn't get updated, but if I do $location.path("/") it's working, but I actually need to refresh the whole page, so I need to use window.location – Jorge Feb 22 '15 at 18:30
  • Maybe it's a digest problem. Try something like `$scope.$apply(function(){ window.location = "/"; })`. – Reactgular Feb 22 '15 at 21:59