User accounts in three easy steps
Meteor rolls its own user accounts system, and getting it running on your application is a three step process:
1 2 3 4 5 6 7 8
1 2 3
1 2 3
While the server side and client side code looks similar they are definitely not interchangeable. Server code run on the client side will likely fail silently, but client code run on the server will crash the app. One place where I got tripped up was accessing the userId:
this is called in the context of a Meteor object, and you can therefore make inquiries about its properties. Use this.userId inside the server publish function to access the id of the current user (or null if no user is logged in — from the docs).
In the Meteor client side,
this has a different context: the
window object. The window object has a userId property that can be set at Document.ready(), but in the case that a user logs out and another user logs in to that same window without a page refresh,
window.userId will retain the id of the first user. Use Meteor.userId() anywhere BUT in the server publish function to get the current user id (or null if no user is logged in — from the docs).