【问题标题】:The right way to verify email in Meteor在 Meteor 中验证电子邮件的正确方法
【发布时间】:2019-02-07 05:13:18
【问题描述】:

我正在试用Meteor的邮箱验证功能,关注Meteor #20: Verify an Email with Meteor Accounts

我对第 3 步和第 4 步有一些疑问:

  1. 在第 3 步中,Accounts.sendVerificationEmail(user._id); 不起作用,而 Accounts.sendVerificationEmail(user._id, user.emails[0].address); 起作用。为什么我需要明确指定电子邮件地址?

  2. 在第4步中,我应该将Template.Homepage.created = ...中的“主页”修改为我的主页模板名称吗?

  3. 在 Meteor 1.0.4 的文档中,我在“Template.myTemplate”下找不到名为“created”的属性。 那么如果Template.Homepage.created = ... 已被弃用? 写成Template.<myhomeTemplateName>.onCreated = ...对吗?

  4. 这是验证电子邮件的最佳解决方案吗?

【问题讨论】:

  • 对于第 1 步,您是否提供了 MAIL_URL 环境变量?
  • @ajduke 是的,我在 Meteor.startup 函数中添加了 process.env.MAIL_URL = 'smtp://...

标签: email meteor


【解决方案1】:
  1. 来自Meteor docs,如果未指定电子邮件,则用户的电子邮件必须在emails 中,并且必须未经验证。如果您已经验证了地址,除非明确指定地址,否则不会发送电子邮件。

    电子邮件字符串 可选的。 将电子邮件发送到用户的哪个地址。此地址必须在用户的电子邮件列表中。默认为列表中第一封未验证的电子邮件。

  2. docs 看来,onCreated 应该这样调用:
    Template.HomePage.onCreated(function(){...});

【讨论】:

    【解决方案2】:

    对于 Meteor 1.0.5,使用

    Template.<yourTemplate>.onCreated(function() { ... })
    

    而不是

    Template.<yourTemplate>.created = ...
    

    Meteor #20: Verify an Email with Meteor Accounts 的第 4 步中。

    下面的演示在 Meteor 1.0.5 下运行良好。

    验证电子邮件.html:

    <head>
      <title>Verify Email</title>
    </head>
    
    <body>
      <h1>Sending Email Demo</h1>
      {{> loginButtons}}
    </body>
    

    verifyEmail.js:(为您的案例替换用户、密码和服务器值)

    if (Meteor.isClient) {
      Template.SendAnyEmail.onCreated(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.')
            }
          });
        }
      });
    }
    
    if (Meteor.isServer) {
      Meteor.startup(function () {
        smtp = {
          username: 'user@example.com',
          password: 'password',
          server: 'mail.example.com',
          port: 465
        };
    
        process.env.MAIL_URL = 'smtp://' + encodeURIComponent(smtp.username) + ':' + encodeURIComponent(smtp.password) + '@' + encodeURIComponent(smtp.server) + ':' + smtp.port;
    
        Accounts.emailTemplates = {
          from: 'Administrator <user@example.com>',
          siteName: 'YourSite',
          verifyEmail: {
            subject: function(user) {
              return 'Verification email from Example.com';
            },
            text: function(user, url) {
              return 'Hi,\n' +
                'Please open the link below to verify your account on Example.com:\n' + url;
            }
          }
        };
      });
    
      Accounts.onCreateUser(function(options, user) {
        Meteor.setTimeout(function() {
          Accounts.sendVerificationEmail(user._id);
        }, 2 * 1000);
        return user;
      });
    }
    

    【讨论】:

      【解决方案3】:

      在您的server/main.js 中,只需设置Meteor.startup 块之外的任何位置。

      Accounts.config({
          sendVerificationEmail: true
      });
      

      【讨论】:

        猜你喜欢
        • 2019-05-02
        • 1970-01-01
        • 2014-04-15
        • 1970-01-01
        • 2014-04-15
        • 1970-01-01
        • 2015-12-20
        • 2022-08-13
        • 2022-12-11
        相关资源
        最近更新 更多