Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Persistent Sessions in Meteor

So, one of the more confusing aspects I've been observing with Meteor is that Sessions get cleared every refresh. Since it isn't a persistent store, where would I put things like userid, or where people are in my application's state machine?

What are the patterns for those scenarios?

like image 208
user1560338 Avatar asked Jul 29 '12 00:07

user1560338


2 Answers

Actually what you could do is create a "subclass" of Session that stores the value in Amplify's local storage when set() is called. You would automatically inherit all the reactive properties of Session. Here is the code, it worked for me:

SessionAmplify = _.extend({}, Session, {
  keys: _.object(_.map(amplify.store(), function(value, key) {
    return [key, JSON.stringify(value)]
  })),
  set: function (key, value) {
    Session.set.apply(this, arguments);
    amplify.store(key, value);
  },
});

Just replace all your Session.set/get calls with SessionAmplify.set/get calls. When set() is called, the parent Session method is called, as well as amplify.store(). When the "subclass" is first created, it loads everything that is in amplify's store inside its keys, so that they can be retrieved right away with get().

You can test a working variation of the Leaderboard example here: https://github.com/sebastienbarre/meteor-leaderboard

like image 139
sebastien.b Avatar answered Oct 12 '22 09:10

sebastien.b


Well, for a start I would be using meteors built in Auth to store userID. They are using local storage by default there I think, but AFAIK there's no easy way to hook into that.

However, I would have thought if you want stuff to survive across refreshes you should either store it in mongo or use the URL to indicate where they are in the 'state machine'. You can use the bootstrap router (for example) to use pushState to change the URL.

like image 42
Tom Coleman Avatar answered Oct 12 '22 09:10

Tom Coleman