I'm using Meteor 0.6.3.1 and have improvised my own user system (not really a user system but I thought I might as well make use of the userId variable since nobody else laid claim to it.)
The problem is, the variable isn't persisting.
I have this code
Meteor.methods({
'initCart': function () {
console.log(this.userId);
if(!this.userId) {
var id = Carts.insert({products: []});
this.setUserId(id);
console.log("cart id " + id + " assigned");
}
return this.userId;
}
});
The point being, you should be able to switch pages but still use the same shopping cart.
I can't use Sessions since they're client-side and could lead to information leaking between users..
How should I go about doing this? Is there anything like Amplify for server-side Meteor?
From Meteor docs:
setUserId is not retroactive. It affects the current method call and any future method calls on the connection. Any previous method calls on this connection will still see the value of userId that was in effect when they started.
When you refresh you create a new connection. On that connection you log-in using the cookie stored by the user system on the client side.
You can store the cart id in a cookie...
This works for me:
# server/methods/app.coffee
#---------------------------------------------------
# Info found in Meteor documentation (24 Feb. 2015)
#
# > Currently when a client reconnects to the server
# (such as after temporarily losing its Internet connection),
# it will get a new connection each time. The onConnection callbacks will be called again,
# and the new connection will have a new connection id.
# > In the future, when client reconnection is fully implemented,
# reconnecting from the client will reconnect to the same connection on the server:
# the onConnection callback won't be called for that connection again,
# and the connection will still have the same connection id.
#
# In order to avoid removing data from persistent collections (ex. cartitems) associated with client sessionId (conn.id), at the moment we'll implement the next logic:
#
# 1. Add the client IP (conn.clientAddress) to the cartitems document (also keep the conn.id)
# 2. When a new connection is created, we find the cartitems associated with this conn.clientAddress and we'll update (multi: true) the conn.id on the all cartitems.
# 3. Only remove the cartitems associated with the conn.id after 10 seconds (if in this time the customer reconnect, the conn.id will have been updated at the point #2. we avoid this way removing after refresh the page for example.
# 4. After 10 seconds (ex. the user close the window) we'll remove the cartitems associated with the conn.id that it has been closed.
Meteor.onConnection (conn) ->
CartItems.update({clientAddress: conn.clientAddress}, {$set: {sessionId: conn.id}}, {multi: true})
conn.onClose ->
Meteor.setTimeout ->
CartItems.remove {sessionId: conn.id}
, 10000
Meteor.methods
getSessionData: ->
conn = this.connection
{sessionId: conn.id, clientAddress: conn.clientAddress}