Creating a validation token in Meteor without sending an email?

In my meteor application, I am setting up the registration process.

Meteor has an Account.sendVerificationEmail method to send email to new token users to verify their email address

My application needs this functionality, but I really don't want to use sendVerificationEmail to send emails, because I already have my own email assistant that has a ton of logic, and I want all emails on my system to go through to flow out this function.

So my question is that I want to create a verification token for the user during registration, but I do not want sendVerificationEmail to send an email, because I want to do it manually.

Is it possible?

+3
source share
2 answers

First add the main “random” package to generate random code

$ 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 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 do not want to display pieces of ui elements to unverified users, you can create a template helper for this. Or you can check if the user is verified in your publications. Etc ... everything you want to limit.

Now you can determine the route in your application using the iron router, so that when the user clicks on the link, the route takes a confirmation code and sets the verified user flag to true.

+5
source

MongoDB, :

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

, GitHub:

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

+2

All Articles