Question

In my meteor app, I'm setting up the registration process.

Meteor has a Account.sendVerificationEmail method to send emails out to new users with a token to verify their email address

My app does need this functionality but I don't really want to use the sendVerificationEmail to send the emails because I already have my own email helper which has got a bunch of logic and I want all the emails in my system to pass to flow out of that function.

So my question is that I do want to create my verification token for the user on registration, but I don't want sendVerificationEmail to send an email out because I want to do it manually.

Is this possible?

No correct solution

OTHER TIPS

First add the core "random" package for random code generation

$ meteor add random

Then intercept the account creation process

Accounts.onCreateUser(function(options, user) {
  // create a verified flag and set it false
  user.customVerified = false;

  //20 character random lowercase hex string. You can use a hash of some user info if you like. I just put this here for demonstration of the concept :)
  user.customVerificationCode = Random.hexString(20).toLowerCase(); 

  //pass the new user's email and the verification code to your custom email function so that you can craft and send the mail. Please double check the option.profile.emails[0], the email should be available somewhere within the options object
  myCustomEmailFunction(options.profile.emails[0], user.customVerificationCode); 

  // continue with account creation
  return user;
}); 

At this point, if you don't want to show pieces of ui elements to unverified users, you can create a template helper for that. Or you can check if user is verified in your publications. Etc... whatever you want to restrict.

Now you can define a route in your app with iron router so that when the user clicks on the link, the route takes the verification code and set's the user's verified flag to true.

You can fake the verification record for the user in the MongoDB document and then send your own email:

//Fake the verificationToken by creating our own token
var token = Random.secret();
var tokenRecord = {
    token: token,
    address: Meteor.user().emails[0].address,
    when: new Date(),
};

//Save the user
Meteor.users.update(
    {_id: Meteor.userId()}, 
    {$push: {'services.email.verificationTokens': tokenRecord}}
, function(err){

    //Send an email containing this URL
    var confirmUrl = Meteor.absoluteUrl() + '#/verify-email/' + token;
    //Send using SendGrid, Mandrill, MailGun etc
});

Code taken from GitHub:

https://github.com/meteor/meteor/blob/5931bcdae362e1026ceb8a08e5a4b053ce5340b7/packages/accounts-password/password_server.js

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top