Meteor:在电子邮件验证确认上做点什么 [英] Meteor: Do Something On Email Verification Confirmation

查看:68
本文介绍了Meteor:在电子邮件验证确认上做点什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的服务器上,我将帐户设置为需要电子邮件验证并发送验证电子邮件:

On my server I made accounts require email verification and to send the verification email:

Accounts.config({sendVerificationEmail: true, forbidClientAccountCreation: false});

我在网上某处读到,验证链接会在用户点击后重定向到 Web 应用程序的主页.

I read somewhere online that the verification link will redirect the user to the home page of the web app once clicked.

在该主页上,我尝试捕捉第一次得到确认的信息,因为我想在第一次电子邮件得到验证并且用户得到身份验证时向 MONGO 数据库添加一些条目.

On that home page I try to catch the first time it gets confirmed as I would like to add some entries to the MONGO DB the FIRST TIME the email gets verified and the user gets authenticated.

所以我尝试通过这样做在客户端获得此确认:

So I try to get this confirmation on the client side by doing this:

Template.home.created = function(){
      if (Accounts._verifyEmailToken) {
        Accounts.verifyEmail(Accounts._verifyEmailToken, function(err) {
          if (err != null) {
            if (err.message = 'Verify email link expired [403]') {
              console.log('Sorry this verification link has expired.')
            }
          } else {
            console.log('Thank you! Your email address has been confirmed.')

          }
        });
      }
    }

不幸的是,我从来没有让 console.log('谢谢!您的电子邮件地址已被确认.') 登录控制台.....我总是收到 console.log('Sorry this verify link has expired.') 即使在我第一次点击它之后.

Unfortunately I NEVER got console.log('Thank you! Your email address has been confirmed.') to log in the console..... I always get console.log('Sorry this verification link has expired.') even after the first time I click on it.

我在这里错过了什么???

What am I missing here???

如何在第一次验证电子邮件时调用函数???

How do I get a function to be called the first time the email gets verified???

谢谢.

推荐答案

两个可能的解决方案:

使用monkey patching来拦截对传递给verifyEmail()的回调的调用,这样你就可以在调用原始回调之外做你想做的.像这样的东西(未经测试):

Use monkey patching to intercept the call to the callback passed to verifyEmail() so that you can do what you want in addition to calling the original callback. Something like this (untested):

Accounts.verifyEmail = _.wrap(Accounts.verifyEmail, function (origVerifyEmail, token, callback) {
  return origVerifyEmail.call(Accounts, token, _.wrap(callback, function (origCallback, err) {
    try {
      if (! err) {
        // Add entries to the DB here
      }
    } finally {
      return origCallback.apply(null, _.rest(arguments));
    }
  }));
});

请注意,如果您使用上述方法,您大概仍需要服务器来确保用户的电子邮件地址实际上已验证(即用户的emails数组在实际向数据库添加内容之前,包含一个带有 verified: true 的对象.因此,您可能更喜欢...

Note that if you use the above approach, you will presumably still need the server to ensure that the user's email address is in fact verified (i.e. the user's emails array contains an object with verified: true) before actually adding stuff to the DB. For that reason, you might prefer...

在服务器上,查看 Meteor.users 集合以了解电子邮件地址验证状态的更改.像这样的东西(未经测试):

On the server, watch the Meteor.users collection for changes to the verification status of email addresses. Something like this (untested):

Meteor.users.find().observe({
  changed: function (oldUser, newUser) {
    if (! _.findWhere(oldUser.emails, { verified: true }) &&
      _.findWhere(newUser.emails, { verified: true })) {
      // Add entries to the DB here
    }
  }
});

这篇关于Meteor:在电子邮件验证确认上做点什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆