【问题标题】:Meteor Accounts-ui to wait for email verification before user accessMeteor Accounts-ui 在用户访问之前等待电子邮件验证
【发布时间】:2016-10-22 02:46:52
【问题描述】:

本地计算机上的 Meteor Web 应用程序使用 Accounts-passwordAccounts-ui
用户输入电子邮件和密码以创建新帐户。

需要进行配置,以便用户只有在通过电子邮件验证帐户的创建后才能获得 userId(从而成为 currentUser)。

应用程序设置了一个 smtp 变量。它可以很好地发送验证电子邮件。但它未能阻止用户在验证之前“登录”。怎样才能让它在验证后只给出一个有效的 userId 呢?谢谢

//server
Accounts.onCreateUser(function(options, user) {
  Accounts.config({
    sendVerificationEmail: true
  });
  return user;
});

【问题讨论】:

  • 首先,您不需要在每次创建用户时都调用Accounts.config()。可以通过Accounts.validateLoginAttempt 控制登录。
  • @MasterAM 不是 onCreateUser 吗?
  • 这是 2 个不同的 API。您应该阅读文档以了解您可以做什么。比猜测好。你说你想阻止登录,所以这就是你需要定位的 API 调用。
  • @MasterAM 没有关于可以从中榨出多少汁液的示例和教程的文档。知道如何在我的用例中使用它吗?

标签: meteor meteor-accounts


【解决方案1】:

我定义了一个全局助手:

Template.registerHelper('isVerified',function(){ // return a true or false depending on whether the referenced user's email has been verified
  if ( Meteor.user() && Meteor.user().emails ) return Meteor.user().emails[0].verified; // look at the current user
  else return false;
});

然后在任何模板(通常是我的主模板)中我都可以:

{{#if isVerified}}
  content that only verified users should see
{{else}}
  Please check your email for your verification link!
{{/if}}

【讨论】:

  • 这段代码的一个问题是,如果用户没有记录,它将运行{{else}} 块。
  • 这种方法的问题在于它很容易绕过。这是否足够取决于电子邮件验证的重要性(如果这只是提醒用户验证电子邮件的“轻推”,那是合理的)。
【解决方案2】:

正如@Season 所指出的,验证需要创建一个 userId 才能将其链接到验证电子邮件。

您可以做的是阻止用户在未验证电子邮件的情况下登录。请参阅另一个问题的答案,它实现了这一点:

https://stackoverflow.com/a/24940581/3512709

【讨论】:

    【解决方案3】:

    事情没有这么简单。 Meteor 需要用户拥有一个 ID 才能向他们发送电子邮件。您想要做的可能不是实际上阻止登录,而是阻止为了做您想做的事情,您必须阻止他们在他们登录时看到任何东西。像这样的东西在您的顶级模板中:

    {{#if userVerified}}
      // Body of your app here
      {{> Template.dynamic template=main}}
    {{else}}
      You are not yet verified
    {{/if}}
    

    这在相应的.js 文件中:

    Template.body.helpers({
      userVerified () {
        const user = Meteor.user();
        return user.emails[0].verified;
      }
    });
    

    【讨论】:

    • user.emails.verified = false,但 {{#if currentUser}} 正在返回 true。怎么会?
    • 天啊!它返回 true 因为他们已登录。我更新了 sn-p 以改为仅在它们也经过验证时才返回 true。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 2019-02-13
    • 1970-01-01
    相关资源
    最近更新 更多