流星:在电子邮件验证确认上做点什么

时间:2015-09-23 06:40:18

标签: javascript node.js meteor meteor-accounts

在我的服务器上,我创建的帐户需要验证电子邮件并发送验证邮件:

require.config({

    paths: {
        'angular': '../lib/angular/angular'
    },

    shim: {
        'angular': {
            exports: 'angular'
        }
    }
});

我在网上看到,点击后验证链接会将用户重定向到网页应用的主页。

在该主页上,我尝试第一次收到确认信息,因为我想在MONGO数据库中添加一些条目,第一次验证电子邮件并验证用户身份。

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

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

不幸的是我从来没有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('Thank you! Your email address has been confirmed.')

我在这里缺少什么?

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

谢谢。

4 个答案:

答案 0 :(得分:1)

您的错误消息验证错误:您正在进行分配而不是条件检查。

if (err.message = 'Verify email link expired [403]') // WRONG!
if (err.message == 'Verify email link expired [403]') // this is a condition

我建议您输出err.message的内容以继续前进,因为它可能与链接到期无关!

Template.home.created = function(){
  if (Accounts._verifyEmailToken) {
    Accounts.verifyEmail(Accounts._verifyEmailToken, function(err) {
      if (err != null) {
        console.log(err.message);
      } else {
        console.log('Thank you! Your email address has been confirmed.')
      }
    });
  }
}

答案 1 :(得分:1)

好的......在玩完选项之后......我发现这个有用了。 唯一的缺点是控制台中会出现警告。这是警告:

Accounts.onEmailVerificationLink was called more than once. Only one callback added will be executed.

我相信这是因为我正在使用account-ui package .....并且也许account-ui也使用Accounts.onEmailVerificationLink,现在我们正在覆盖它......

这是解决方案:

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

    if(Meteor.isClient){

      Accounts.onEmailVerificationLink(function(token, done){
        console.log('inside onEmailVerificationLink');
        console.log('token');
        console.log(token);

        Accounts.verifyEmail(token, function(err) {
          if (err != null) {
            console.log(err.message);
          } else {
            console.log('Thank you! Your email address has been confirmed.')
          }
        });

      });
    }


    if(Meteor.isServer){
      Accounts.onCreateUser(function(options, user){

         console.log('inside onCreateUser');
         return user;
      });

    }

答案 2 :(得分:1)

两种可能的解决方案:

解决方案#1

使用猴子修补拦截对传递给verifyEmail()的回调的调用,这样除了调用原始回调之外,您还可以执行所需操作。像这样(未经测试):

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

请注意,如果您使用上述方法,您可能仍需要服务器以确保用户的电子邮件地址实际上已经过验证(即用户{{在向DB实际添加内容之前,{1}}数组包含一个emails}的对象。出于这个原因,你可能更喜欢......

解决方案#2

在服务器上,观看verified: true集合,了解电子邮件地址验证状态的更改。像这样(未经测试):

Meteor.users

答案 3 :(得分:0)

If an email is already verified, verifyEmail function will return the Error: Verify email link expired [403] error.

Make sure that you are only sending the verification token to an unverified email address, if you call the resetPassword function the email will automatically be verified.

It could be that you tested the verification token once on an account and it verified the email and then when you tried again it gave you the link expired error.

A good way to test if the email is verified is to add the following snippet of code to your dashboard (or wherever the page goes when {{#if currentUser}} condition is true):

<p>
    {{#if currentUser.emails.[0].verified}}
        <p>Email is verified</p>
    {{else}}
        <p>Email is not verified</p>
    {{/if}}
</p>

Hope this helps!