【问题标题】:Meteor: Accounts.sendVerificationEmail customising behaviorMeteor:Accounts.sendVerificationEmail 自定义行为
【发布时间】:2015-01-30 14:16:53
【问题描述】:

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

a) 我希望用户在注册后能够立即访问。但是如果用户在 48 小时内还没有点击验证链接,我想在他们点击链接之前拒绝他们登录。

到目前为止,我的代码发送了一封电子邮件验证,但无论是否点击验证链接,用户都可以继续访问应用程序(所以我的代码当然是不完整的)。

client.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');
       }
    });
   }
});

server.js

Meteor.startup(function () {
process.env.MAIL_URL = 'smtp://postmaster.....';
Accounts.emailTemplates.from = "no-reply@mydomain.com";
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
});

我的尝试是通过自己阅读流星文档并查看 SO 上的其他代码进行的。我被困住了。感谢您的支持。

【问题讨论】:

    标签: javascript meteor email-verification meteor-accounts


    【解决方案1】:

    我认为基本的想法是在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));
          }
        });
      }
    };
    

    验证链接在“钩子”中发送到Accounts.createUser

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

    【讨论】:

    • 公平地说,48 小时宽限期不必那么严格。只要在某个时候对用户的电子邮件地址进行了验证。我看到 Accounts.validateLoginAttempt 有一个“允许的”回调函数,它返回真或假。首先,你能告诉我如何设置 LoginWithEmail。我不明白当用户点击链接时它当前做了什么。 +1 对问题的有条不紊的方法,谢谢Steffo
    • 重新编辑了答案。当用户点击验证链接(看起来像localhost:3000/#/verify-email/lots-of-funny-chars)时,login 模板被创建,Accounts.verifyEmail 抓取有趣的字符并验证它们(Meteor 负责这一点)。
    • 感谢您的意见。我将应用此代码,并让您知道我很快就会开始并相应地投票。
    • 这很有帮助。我仍在尝试解构正在发生的事情。我想做的是回去简化。你能告诉我一种向用户发送电子邮件验证链接的干净方法吗?请问就这些了?
    猜你喜欢
    • 2013-04-18
    • 2012-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-18
    • 2012-04-13
    • 1970-01-01
    相关资源
    最近更新 更多