javascript - Meteor:Accounts.sendVerificationEmail 自定义行为

标签 javascript meteor email-verification meteor-accounts

有人可以提供在创建用户时发送电子邮件验证的正确方法吗?这是重要的部分...

a) 我希望用户在注册后能够立即访问。但如果用户在 48 小时内尚未点击验证链接,我想拒绝他们登录,直到他们点击该链接。

到目前为止,我的代码会发送电子邮件验证,但用户无论单击验证链接还是不单击验证链接都可以连续访问该应用程序(因此我的代码当然不完整)。

客户端.js

Template.join.events({
'submit #join-form': function(e,t){
 e.preventDefault();
  var firstName=  t.find('#join-firstName').value,
  lastName=  t.find('#join-lastName').value,
  email = t.find('#join-email').value,
  password = t.find('#join-password').value,
  username = firstName.substring(0) + '.' + lastName.substring(0),
  profile = {
    fullname: firstName + ' ' + lastName
  };
  Accounts.createUser({
    email: email,
    username: username,
    password: password,
    userType: // 'reader' or 'publisher'
    createdAt: new Date(),
    profile: profile
 }, function(error) {
if (error) {
  alert(error);
} else {
  Router.go('home');
       }
    });
   }
});

服务器.js

Meteor.startup(function () {
process.env.MAIL_URL = 'smtp://postmaster.....';
Accounts.emailTemplates.from = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3a545517485f4a56437a57435e55575b535414595557" rel="noreferrer noopener nofollow">[email protected]</a>";
Accounts.emailTemplates.sitename = "My SIte Name";

Accounts.emailTemplates.verifyEmail.subject = function(user) {
  return 'Please confirm tour Email address' ;
},
Accounts.emailTemplates.verifyEmail.text = function(user, url) {
  return 'Click on the link below to verify your address: ' + url;
}
Accounts.config({
  sendVerificationEmail: true
});

我的尝试是通过自己阅读 Meteor 文档并查看 SO 上的其他代码来完成的。我被困住了伙计们。感谢您的支持。

最佳答案

我认为基本的想法是在Accounts.validateLoginAttempt中添加一些验证代码,您希望每次在用户登录之前检查这些验证代码。您可以做的是存储用户登录时的日期和时间在 user.profile.joinDate 中。如果用户尝试登录

  • 检查电子邮件地址是否已验证或
  • 检查用户是否在 48 小时的宽限期内登录
isWithinGracePeriod = function(user) { 
      ** TBD returning true or false. 
         This can be tricky when you 
         have multiple instances in 
         different time-zones. 
      ** }

Accounts.validateLoginAttempt(function(attempt){
  if (attempt.user && attempt.user.emails && !attempt.user.emails[0].verified ) {
    console.log('No verification action received yet.');
    return isWithinGracePeriod(attempt.user); 
  }
  return true;
});

此外,这是 HTML/空格键的内容:

<body>
    {{ > start }}
</body>

<template name="start">
  {{#if currentUser}}{{>showUserProfile}}{{else}}{{> login}}{{/if}}
</template>

<template name="login">
   ## Grab username/password here
</template>

如果创建了login模板,我们可以尝试在用户点击验证链接后捕获验证码。请注意,如果没有用户登录,则将呈现 login,因此我们通过

附加到 login
Template.login.created = function() {
  if (Accounts._verifyEmailToken) {
    Accounts.verifyEmail(Accounts._verifyEmailToken, function(err) {
      if (err != null) {
        if (err.message = 'Verify email link expired [403]') {
          var message ='Sorry this verification link has expired.';
          console.log(message);    
          alertBox = Blaze.renderWithData(Template.Alert, {message: message}, $("body").get(0));
        }
      } else {
        var message = "Thank you! Your email address has been confirmed.";
        console.log(message);
        alertBox = Blaze.renderWithData(Template.Alert, {message: message}, $("body").get(0));
      }
    });
  }
};

验证链接以“hook”形式发送到 Accounts.createUser:

Accounts.onCreateUser(function(options, user) {
   user.profile = {};
   Meteor.setTimeout(function() {
   Accounts.sendVerificationEmail(user._id);
     }, 2 * 3000);
  return user;
});

关于javascript - Meteor:Accounts.sendVerificationEmail 自定义行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27247648/

相关文章:

PHP 准备语句;奇怪的结果

java - 无法使用selenium绕过电子邮件验证步骤

javascript - Javascript 中数学对象的速度

javascript - 为什么 getElementById 不适用于文档以外的元素?

javascript - 如何在javascript中合并具有相同值的json数组

javascript - 无法通过 JavaScript 更改 <DIV> 的 css 可见性

javascript - 电子邮件地址是来自 Meteor JS 中的事件监控 API 的无效响应

arrays - 在数组上执行 findOne() 时,Meteor 和 Mongo DB 会提供不同的结果

meteor - 如何管理同步到 Meteor 客户端缓存的数据?

javascript - Meteor:process.env.MAIL_URL 敏感信息