简体   繁体   中英

Accounts.onLogin how to get user Id?

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

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

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());
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM