Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accounts.onLogin how to get user Id?

Tags:

meteor

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

Upon user creation, the user is automatically signed into the application. Is the user that is returned by the Accounts.onCreateUser function occurring after the user is logged in?

Accounts.onLogin(function(){

 var user = this.userId / Meteor.user() / Meteor.user()._id 
 console.log(user)

})

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

like image 426
meteorBuzz Avatar asked Mar 30 '15 18:03

meteorBuzz


2 Answers

The Accounts.onLogin(function(){}), come with 1 parameter user.

When it is known which user was attempting to login, the Meteor user object. This will always be present for successful logins.

from the docs.

So this should work.

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

And you should see, all the user document into the server console,check this meteorpad for an example.

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

like image 141
Ethaan Avatar answered Oct 31 '22 01:10

Ethaan


You can always get the _id of the logged-in user via Meteor.userId(). This also works inside the Accounts.onLogin callback

Accounts.onLogin(function() {
  console.log(Meteor.userId());
})
like image 39
EthanP Avatar answered Oct 31 '22 01:10

EthanP