Open SudoPlz opened 8 years ago
Also local storage doesn't seem to work for me either... Tried both on Chromium and Firefox (OSX)
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?
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?
Does it work if you use localStorage directly without using the state source?
How can I do this, would you mind giving some code?
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.
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 ?
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.
I'm using
this.app.session.setToken(tok);
andthis.app.session.getToken(tok);
fromthis 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