在Meteor中生成验证令牌而不发送电子邮件?

时间:2014-02-13 11:36:21

标签: email meteor verification

在我的流星应用程序中,我正在设置注册过程。

Meteor有一个Account.sendVerificationEmail方法,可以通过令牌向新用户发送电子邮件,以验证他们的电子邮件地址

我的应用程序确实需要此功能,但我真的不想使用sendVerificationEmail发送电子邮件,因为我已经拥有自己的电子邮件帮助程序,它有一堆逻辑,我希望系统中的所有电子邮件都通过流出这个功能。

所以我的问题是我确实想在注册时为用户创建验证令牌,但我不希望sendVerificationEmail发送电子邮件,因为我想手动完成。

这可能吗?

2 个答案:

答案 0 :(得分:5)

首先添加核心“随机”包以进行随机代码生成

$ meteor add random

然后拦截帐户创建过程

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

此时,如果您不想向未经验证的用户显示ui元素片段,可以为其创建模板助手。或者您可以检查您的出版物中是否验证了用户。等等......无论你想限制什么。

现在,您可以使用铁路由器在应用中定义路线,以便当用户点击链接时,路线会获取验证码并将用户的已验证标记设置为true。

答案 1 :(得分:2)

您可以在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