416serg
416serg

Reputation: 448

Meteor user account email verification

I am trying to set up email verification for users. I am using useraccounts:core's enforceEmailVerification and i have the following on my server

Accounts.onCreateUser(function(options, user) {
  var userId = user._id;
  Accounts.sendVerificationEmail(userId);

  if(options.profile.invite){
    Invites.remove({_id: options.profile.invite});
  }

  user.profile = options.profile

  return user;
});

When I try to sign up as a user i get the following server error

I20150206-18:12:08.648(-5)? Exception while invoking method 'ATCreateUserServer' Error: Can't find user
I20150206-18:12:08.648(-5)?     at Object.Accounts.sendVerificationEmail (packages/accounts-password/password_server.js:562:1)
I20150206-18:12:08.648(-5)?     at Meteor.methods.deleteAccount.userId (app/server/accountsMeld.js:12:12)
I20150206-18:12:08.648(-5)?     at Object.Accounts.insertUserDoc (packages/accounts-base/accounts_server.js:1024:1)
I20150206-18:12:08.649(-5)?     at createUser (packages/accounts-password/password_server.js:693:1)
I20150206-18:12:08.649(-5)?     at Object.Accounts.createUser (packages/accounts-password/password_server.js:751:1)
I20150206-18:12:08.649(-5)?     at [object Object].Meteor.methods.ATCreateUserServer (packages/useraccounts:core/lib/methods.js:66:1)
I20150206-18:12:08.649(-5)?     at [object Object].methodMap.(anonymous function) (packages/meteorhacks:kadira/lib/hijack/wrap_session.js:182:1)
I20150206-18:12:08.649(-5)?     at maybeAuditArgumentChecks (packages/ddp/livedata_server.js:1599:1)
I20150206-18:12:08.649(-5)?     at packages/ddp/livedata_server.js:648:1
I20150206-18:12:08.649(-5)?     at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)

Upvotes: 0

Views: 1229

Answers (2)

Al Francis
Al Francis

Reputation: 333

You need to put sendVerificationEmail inside Accounts.createUser and set the timeout

Accounts.onCreateUser(function(options, user) {

 // we wait for Meteor to create the user before sending an email
 Meteor.setTimeout(function() {
    Accounts.sendVerificationEmail(user._id);
 }, 2 * 1000);

 return user; 
});

Upvotes: 1

reoh
reoh

Reputation: 302

I came across this problem also and according to this: https://github.com/alanning/meteor-roles/issues/35#issuecomment-40674250 ,

it's not a good idea to put Accounts.sendVerificationEmail(userId); inside Accounts.onCreateUser because the user doesn't yet "exist".

If you're using a method you can return the userID when the user is created, then send the email afterwards, like so:

var userID = Accounts.createUser(options, callback);
Accounts.sendVerificationEmail(userID);

Upvotes: 2

Related Questions