gsklee/ngStorage

How to wait on delete to take place

ronnieoverby opened this issue · 24 comments

In my app, when a user logs out I want to

  1. Delete their auth token from $localStorage.
  2. Reload the page

I noticed that this code wasn't working:

delete $localStorage.authToken;
$window.location.href = '/';

When the page reloads, the auth token is still present.

So, as an experiment I put the reload in a timeout and it worked.

delete $localStorage.authToken;
$timeout(function () {
    $window.location.href = '/';
}, 110);

I also noticed that the timeout had to be greater than 100... which leads me to believe that this has something to do with _debounce section in your code, which I don't quite understand, yet.

What's the best way that I can be sure that the data has been removed from web storage before reloading the page?

Sorry for previous comment. Only read part of your initial comment before responding.

I wonder if they're using the timeout for performance reasons? Trying to avoid too-frequently flooding the [synchronous] storage interface?

Some inline documentation regarding the purpose of _debounce would surely be nice.

+1 having this problem too.
TImeout work around is quite ugly but works.

Just bumped into this too... WTF? Using $timeout now too but this is very unsafe! We need a dedicated function to delete storage data which will return promise, so we can properly wait for it resolving (just an idea).

Btw, you can read about debounce here: http://underscorejs.org/#debounce

+1 for this. Very annoying to have timeouts in the code to prevent something like this.

+1

We just ran into a similar problem with an instant redirect.
Couldn't we have an event that is broadcast when a save occurred or have a $save method that is triggered by the debounced $watch, so we can have a sync way of saving the session?

Another reason to having the $save is not to trigger a $rootScope.$digest when I need to change something on a DOM event.

+1 for an event or some kind of save-method without timeout

I did a fork for this project, as the last commit is 11 months old.

https://github.com/raynode/ngStorage

It already includes a few Issue fixes and I will try to fix more. If you like to use this instead you are welcome to contribute there. However, if @gsklee likes to keep his project up, I could submit a rather large pull request (including the here missing tests)

Very nice.

On Wed, Oct 22, 2014 at 7:53 AM, lordnox notifications@github.com wrote:

I did a fork for this project, as the last commit is 11 months old.

https://github.com/raynode/ngStorage

It already includes a few Issue fixes and I will try to fix more. If you
like to use this instead you are welcome to contribute there. However, if
@gsklee https://github.com/gsklee likes to keep his project up, I could
submit a rather large pull request (including the here missing tests)


Reply to this email directly or view it on GitHub
#39 (comment).

@lordnox Hello. You may want to pull some changes from my fork, too, if you are interested in maintaining this project.

I am interested and did write @gsklee and asked him, but this might take a while.

Would be nice to get the "ngStorage" name on bower, but this will take his okay.

I'm not sure how to pull in your changes, they diverged quite a bit but will see what I can do. If you can I would love pull requests on my fork.

@lordnox Thanks. I'm currently unable to invest time into further development of ngStorage. Please feel free to ask me questions if needed.

Please consider taking a lowercase name for Bower, there were issues with the camelcased variant.

I should take the lower-case name, you are right.

Would you mind sending me an PM or email so i could contact you? I do have some questions about your repository.

@lordnox i've sent you an email.

The $timeout works, but I agree that it's ugly. Anyone using a better solution?

fenos commented

Please someone has found a solution for this that is not the $timeout? I'm having few redirect loops for it with in the same your scenario

I did introduce a solution in my fork https://github.com/raynode/ngStorage

As gsklee seems not to be available I'd propose you to use that repository?

The workaround is something like:

$localStorage.test = 123
// not saved yet, would debounce for 100ms
$localStorage.$save();
// now it was saved immediately
fenos commented

Thank you, I absolutely use your repo, but the issue is not saving the data on the storage, but deleting it.

for example this is what i currently use:

//Handle token expiration
            if (response.status === 403 && response.data.context === 'auth-token') {

                $localStorage.$reset();

                delete $http.defaults.headers.common['Authorization'];

                // if i redirect straight away without the timeout it get in a loop because 
                // i guess it take same time to delete data from the storage
                $timeout(function() {
                    $window.location.href = '/auth/login';
                    $window.location.reload();
                },110);

                return false;
            }

Have you tried this:

//Handle token expiration
            if (response.status === 403 && response.data.context === 'auth-token') {

                $localStorage.$reset();

                delete $http.defaults.headers.common['Authorization'];

                // tell the system to save immediately
                $localStorage.$save();

                // no need for $timeout
                $window.location.href = '/auth/login';
                $window.location.reload();

                return false;
            }

And I finally need to add my rep to bower as ng-storage...

fenos commented

Yes it does work! 👍 Thanks so much, I had to add few $localStorage.$save() method when saving the data on the storage it seem not adding it automatically as before, but It took me 20 seconds so no problem! Great fix

@egilkh is the new maintainer of this repo.

Probably try $location to do the redirect to let digest and such instead of
$window?

On Thursday, June 11, 2015, John Babak notifications@github.com wrote:

@egilkh https://github.com/egilkh is now maintaining this repo.


Reply to this email directly or view it on GitHub
#39 (comment).

Egil Hanger
Heimdal IT AS
egil@heimdalit.no / 95737838