martyjs / marty

A Javascript library for state management in React applications
http://martyjs.org
MIT License
1.09k stars 77 forks source link

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

Open SudoPlz opened 8 years ago

SudoPlz commented 8 years ago

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 commented 8 years ago

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

taion commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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

SudoPlz commented 8 years ago

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

taion commented 8 years ago

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 commented 8 years ago

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 ?

taion commented 8 years ago

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.