Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local storage with multiple tabs #91

Closed
bakura10 opened this issue Feb 25, 2014 · 33 comments
Closed

Local storage with multiple tabs #91

bakura10 opened this issue Feb 25, 2014 · 33 comments
Labels

Comments

@bakura10
Copy link
Contributor

Hi,

I have a strange behaviour, I'm not sure it comes from Ember-Simple-Auth though. If I open my website in one tab, and then open the same in another tab, most of the values from the local storage are gone excepting "access_token" and "authenticator", which in turn throws an exception.

Can you try if you can reproduce the error?

@seifsallam
Copy link

I had this issue and now its fixed in 1.2

@bakura10
Copy link
Contributor Author

Hi,

I updated to 1.2, added the "container" to setup and now... it breaks everything. It does not work at all. When I submit the login form, no data is saved to the local storage anymore. I need to resubmit the form two times. Then if I switch to another tab, it simply reset the local storage :/.

@bakura10
Copy link
Contributor Author

I've fixed a bug in my code, it seems to work now. I'm still leaving this issue open until I can confirm it's resolved :)

@bakura10
Copy link
Contributor Author

It definitely still don't work. When I open the page in another tab, all the fields from the local storage are deleted, excepting those two: ember_simple_auth:access_token and ember_simple_auth:authenticatorFactory

@seifsallam
Copy link

Can you share a Gist of how you setup your authentication

@bakura10
Copy link
Contributor Author

Sure, here is the whole initializer: https://gist.github.com/bakura10/686505e4a63a4df4ff24

@seifsallam
Copy link

Have you tried container.register('app:authenticators:authenticator', Authenticator)?

    Authenticator = Ember.SimpleAuth.Authenticators.OAuth2.reopen({
      serverTokenEndpoint: envObject.env.OAUTH_SERVER_TOKEN_ENDPOINT
    });

Update

Updated gist — https://gist.github.com/seifsallam/5beac9912c54621ea37c

@bakura10
Copy link
Contributor Author

No more luck. It's a bit strange, sometimes other variables from local storage are removed, like refresh token, while other stay there, for no reason :/. I'm wondering if the fact that I'm running this on localhost may have any effect?

@seifsallam
Copy link

Well in my case everything was removed from localstorage, so maybe your case is different than mine. As for localhost, i'm running locally too and there is no problem.

@bakura10
Copy link
Contributor Author

Strange... Which browser are you testing? Can you test in Chrome?

@marcoow
Copy link
Member

marcoow commented Feb 26, 2014

Running on localhost shouldn't be a problem in general; there might be a bug wrt syncing more than one tab though. Could you check whether you're seeing the behavior with the examples as well?

@seifsallam
Copy link

I'm using Chrome too

@seifsallam
Copy link

So Chrome for me works great. But when I tried it now in Safari its not working :|

@bakura10
Copy link
Contributor Author

It's really hard to reproduce... I've got different results each time! Sometimes it works, sometimes the other tab completely reset the whole local storage, sometimes it only clear one or two fields, sometimes it clears most of the fields... I have really no idea of what I'm doing :o.

@marcoow
Copy link
Member

marcoow commented Feb 26, 2014

So it happens for the examples as well?

@bakura10
Copy link
Contributor Author

It does not fail because the examples are simple BUT I did have a lose of some information from local storage. Most of the time, each values from the local storage is removed expect access token and authenticatorFactory.

Now, I understand why MY code fails. Because it relies on some information that are destroyed. In my initializer:

Ember.SimpleAuth.Session.reopen({
      user: function() {
        var userId = this.get('owner_id');

        if (Ember.isNone(userId)) {
          return null;
        }

        return container.lookup('store:main').find('user', userId);
      }.property('owner_id')
    });

However, the "owner_id" property is always removed from local storage when I changed tab. So user returns null, and breaks the whole code. But as the "access_token" is still in the local storage, Ember-Simple-Auth assumes I'm authenticated, hence it tries to get the user, and fails.

@bakura10
Copy link
Contributor Author

I'm trying using the Cookie store, and it seems to work properly. No data is getting removed from the cookies! So it seems the bug is clearly coming from LocalStorage.

@marcoow
Copy link
Member

marcoow commented Feb 26, 2014

Thanks for the research - will look into it in the next days.

@marcoow marcoow added this to the 0..2.0 milestone Feb 26, 2014
@bakura10
Copy link
Contributor Author

Thanks a lot :)

@CodeOfficer
Copy link

I'm not able to get cookie or localStorage to work. Refreshing the page always loses all data.

@marcoow
Copy link
Member

marcoow commented Feb 27, 2014

Would need some more information. Are you using a custom authenticator? Did you upgrade to 0.1.2 and register the authenticator as shown in the README?

@marcoow
Copy link
Member

marcoow commented Feb 27, 2014

Should be fixed in master - unfortunately that stuff is a win to debug...

@marcoow marcoow removed this from the 0..2.0 milestone Feb 27, 2014
@marcoow marcoow closed this as completed Feb 27, 2014
@bakura10
Copy link
Contributor Author

Thanks. I'm gonna try and let you know.

@bakura10
Copy link
Contributor Author

Hi. It seems much more stable, however I still have values from the local storage that disappear for no reason. I've been unable to reproduce the case, it just happens from times to times, for no reason :/.

@marcoow
Copy link
Member

marcoow commented Feb 27, 2014

Can you setup a jsbin maybe that shows a case where values are lost?

@CodeOfficer
Copy link

Update: the problem I described above was my own fault. I had not implemented restore in my custom authenticator which extended Ember.SimpleAuth.Authenticators.Base ... the default behavior of this method is to reject the restore attempt. My bad!

@seifsallam
Copy link

@marcoow I've update to 0.1.3 and i'm still getting this issue in Safari only. Data gets synced across multiple tabs then i get signed out.

Also this is how data looks like in localstorage.
screen shot 2014-03-01 at 10 53 54 am
It goes on for another page, is this correct?

@marcoow
Copy link
Member

marcoow commented Mar 1, 2014

There's definitely sth. very wrong. Looks like the jQuery object gets stored or so. Can you share your code somewhere?

@seifsallam
Copy link

@marcoow
Copy link
Member

marcoow commented Mar 1, 2014

I think the problem is probably the Ember.$.extend here: https://gist.github.com/seifsallam/5beac9912c54621ea37c#file-auth-coffee-L30 which should be unnecessary anyway as the server's response is probably a JSON object anyway.

Also the else case here https://gist.github.com/seifsallam/5beac9912c54621ea37c#file-auth-coffee-L34 will log the user out as it resolves with the passed in credentials which will lead the session to replace all its properties with these credentials effectively deleting the access_token..

I think it would make more sense for you to extend the OAuth 2.0 authenticator and simple override serverTokenEndpoint and makeRequest - sty. like this:

Ember.SimpleAuth.Authenticators.OAuth2.reopen({
  serverTokenEndpoint: '<your-endpoint>',
  makeRequest: function(data) {
    data.email = data. username;
    return this._super(data);
  }
});

@seifsallam
Copy link

@marcoow Thanks it worked. Ember.$.extend was the problem.

The else case is used by the signup and oauth. After the signup or oauth requests succeeds, they return the same data as the login so i just store these data locally.

As for OAuth. I used it before, but i found a lot of stuff that i didn't need.

@erichaus
Copy link

@marcoow hello, I am having similar issue in a fork of ember-cli-simple-auth-token... I was just about to submit my PR when I discovered that opening a 2nd tab instantly boots me out of both tabs...

where should I even start to look for what causes that? any suggestions on what it could be? https://github.com/erichonkanen/ember-cli-simple-auth-token

thanks!

@marcoow
Copy link
Member

marcoow commented Feb 10, 2015

@erichonkanen ember-cli-simple-auth-token isn't actually part of Ember Simple Auth but an extension library that's maintained separately here.

The kind of problem you describe is usually caused by custom authenticators with a missing or wrong implementation of the restore method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants