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

I can't get Marty session storage to persist between tabs or Marty localStorage to work at all! #366

Open
SudoPlz opened this issue Jul 22, 2015 · 8 comments

Comments

@SudoPlz
Copy link

SudoPlz commented Jul 22, 2015

I'm using this.app.session.setToken(tok); and this.app.session.getToken(tok); from

import Marty from 'marty';

class Session extends Marty.SessionStorageStateSource {
  setToken(token) {
    this.set('token', token);
  }

  getToken() {
    return this.get('token');
  }

  logout() {
    this.clear('token');
  }

  clear(key) {
    this.storage.clear(key);
  }
}

export default Session;

this state source.

All works well while I'm in one tab. But when I open another tab the session is lost, and getToken ALWAYS Returns null. Why?

I'm using Marty 0.10.4

@SudoPlz
Copy link
Author

SudoPlz commented Jul 22, 2015

Also local storage doesn't seem to work for me either... Tried both on Chromium and Firefox (OSX)

@taion
Copy link
Member

taion commented Jul 23, 2015

From MDN:

Opening a page in a new tab or window will cause a new session to be initiated, which differs from how session cookies work.

Do you have any more details on what you're seeing with localStorage?

@SudoPlz
Copy link
Author

SudoPlz commented Jul 23, 2015

  1. Aha got it, then sessions only persist through one tab. I'll have to use cookies to share info through other tabs correct?

  2. About local storage, this is what I do:
    I'm using this.app.localStorage.setToken(token); and this.app.localStorage.getToken();
    from

import Marty from 'marty';

class LocalStorage extends Marty.LocalStorageStateSource {
  setToken(token) {
    this.set('token', token);
  }

  getToken() {
    return this.get('token');
  }

  logout() {
    this.clear('token');
  }

  clear(key) {
    this.storage.clear(key);
  }
}

export default LocalStorage;

The token never seems to be saved within localStorage though, what could I be doing wrong?

@taion
Copy link
Member

taion commented Jul 23, 2015

Does it work if you use localStorage directly without using the state source?

@SudoPlz
Copy link
Author

SudoPlz commented Jul 23, 2015

How can I do this, would you mind giving some code?

@taion
Copy link
Member

taion commented Jul 23, 2015

Check the code here - https://github.com/martyjs/marty-lib/blob/v0.10.6/src/local-storage-state-source/localStorage.js

It's pretty straightforward, just replace your state source calls with calls to window.localStorage as shown. I suspect that you just don't have localStorage available for some reason.

@SudoPlz
Copy link
Author

SudoPlz commented Jul 24, 2015

Ok bare in mind my app is isomorphic. *I run the same code on both the server and the browser. *

That being said, I tried to window.localStorage.setItem('test', 'testing123'); and later on from another tab do window.localStorage.getItem('test'); and it worked.

I get testing123 on all tabs I run this code.

This is how my class looks right now. I stopped using LocalStorageStateSource at all now. But I'd love to be able to use that.

import Marty from 'marty';

class LocalStorage {
  setToken(token) {
      return window.localStorage.setItem('token', token);
  }

  getToken() {
      return window.localStorage.getItem('token');
  }

  logout() {
      window.localStorage.clear('token')
  }

  clear(key) {
      window.localStorage.clear(key);
  }
}

export default LocalStorage;

Hmm.. this is weird... Have you tested Marty.LocalStorageStateSource code, does it work for you ?

@SudoPlz SudoPlz changed the title Sessions don't persist between tabs. I can't get Marty session storage to persist between tabs or Marty localStorage to work at all! Jul 24, 2015
@taion
Copy link
Member

taion commented Jul 28, 2015

The state source works for me. Try adding some debug logging for the storage property on the state source - maybe it's picking up the noopStorage for some reason.

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

No branches or pull requests

2 participants