Accounts.onLogin, how to get user id?

How do you get the _id of the user who is logged in. I tried the following combinations and I get errors, or undefined

When creating a user, the user automatically subscribes to the application. Is user returned an Accounts.onCreateUser function that occurs after a user logs in?

 Accounts.onLogin(function(){ var user = this.userId / Meteor.user() / Meteor.user()._id console.log(user) }) 

http://docs.meteor.com/#/full/accounts_onlogin

+5
source share
2 answers

Accounts.onLogin(function(){}) , comes with one user parameter.

When it is known which user is trying to log in, the user is a Meteor object. It will always be present for successful logins.

from docs .

So that should work.

 Accounts.onLogin(function(user){ console.log(user.user._id) }); 

And you should see that the entire user document is in the server console, check this meteorpad for an example.

NOTE: this feature is only available on the server side, check this. Accounts.onLogin / onLoginFailure hooks should be available on the client.

+14
source

You can always get the _id of a registered user through Meteor.userId() . This also works inside the Accounts.onLogin callback

 Accounts.onLogin(function() { console.log(Meteor.userId()); }) 
+3
source

Source: https://habr.com/ru/post/1216482/


All Articles