If I'm understanding correctly, you don't actually care about the cookie, you care about having user-specific data.
Comparison to PHP
Meteor clients communicate with the server via DDP which is an abstraction on top of http. Things like 'cookies' don't exist in the DDP level. Rather, you have access to powerful constructs like sync'd database collections and built-in remote procedure calls.
Meteor's Session
object is a client-only concept that is designed for reactivity. It is not persisted between client visits and the server does not have access to it.
The rough equivalent to PHP's SESSION is a Meteor Collection, which is actually more durable than PHP's SESSION because it is persisted to the database.
User-specific data
Tracking user-specific data like you want in Meteor can be broken down into two parts:
- authenticated users
- anonymous users
Re: #1 - authenticated users
As @Tarang and @Cuberto have pointed out, the Meteor Accounts system (ex. accounts-password) has the concept of user-specific data built-in. It creates and manages the Meteor.users
collection for you and provides the Meteor.user()
function for getting an object specific to that user. It even has a built-in method for user-modifiable data in the profile
field of the user object. The profile
field is automatically published and is reactive as well (since Meteor.user() is reactive).
function doSomething () {
var currentUser = Meteor.user(),
profile;
if (!currentUser) {
// handle 'not authenticated' case
} else {
// already logged in
profile = currentUser.profile || {name:'<not set>'};
console.log('user ', profile.name, ' wants to doSomething');
}
}
You can build your own authentication method but that seems like a recipe for disaster. Easier to write a script that converts from your existing DB structure to the Meteor Accounts structure and do it once in a big dump when you are ready to migrate your users over.
So the Meteor convention is:
- User-specific data that the user should be able to modify goes in the
user.profile
field.
Ex. user.profile.firstname
, user.profile.lastname
- User-specific data that is restricted should go on the root
user
object.
Ex. The meteor-roles package stores user roles in a restricted, user.roles
field.
Here are the relevant docs: http://docs.meteor.com/#meteor_user
Re: #2 - anonymous users
Meteor Accounts does not track anonymous users so you will need to track them yourself. You can use various methods to do this but the core is to store some identifying token on the client's machine in client code (either into localStorage or a cookie).
If you don't need to store user-specific data on the server and only want to change client-side stuff, such as what the users see, then you can do everything from the client.
If you need to store data on the server for anonymous users then you'll have to send the identifying token to the server along with each Meteor method call or database interaction (essentially what PHP does with the SESSION cookie). On the server, create a Collection called 'anonymousData' which will contain all of the user-specific info for your anonymous users, keyed by id token. The server-side functions can query that Collection with the id token the client passes to retrieve user-specific info for that user.
Keep in mind that if the user clears their cookies or deletes localStorage that data will be orphaned so some kind of a last-used check is important.