简体   繁体   English

验证码,而不是环回网址

[英]Verification code rather than url in loopback

I am using Loopback as an api for a phone app. 我正在将Loopback用作电话应用程序的api。

How could I change the out of the box verification of an email address when a user signs up. 用户注册后,如何更改电子邮件地址的开箱即用验证。 I need it to be a 4 digit code (instead of a url) so that it's more friendly to the user verifying their account inside the app. 我需要使用4位代码(而不是网址),以便用户在应用程序内验证其帐户时更加友好。

ie. 即。 they would then just need to enter the 4 digit number to confirm the registration 他们只需要输入4位数字即可确认注册

There are a couple of ways to do this, but it basically comes down to overriding the user.verify() method (or some portion of it). 有两种方法可以做到这一点,但是基本上可以归结为覆盖user.verify()方法(或其中的一部分)。 If you click on that link above and scroll down just a bit you'll see how LoopBack is using the crypto.randomBytes() method to generate the verificationToken which is stored on the user object, then it emails it to the user. 如果你点击上方的链接,然后向下滚动只是有点你会看到环回功能如何使用crypto.randomBytes()方法来生成verificationToken其存储在用户对象上,然后它的电子邮件给用户。 We can override this method in a "submodel" which extends user and then implement it ourselves, but note that you would be copying a lot of that information. 我们可以在扩展user的“子模型”中覆盖此方法,然后自己实现该方法,但是请注意,您将复制很多信息。

That said, I've submitted a PR to LoopBack to make this easier. 就是说,我已向LoopBack 提交了PR,以简化此过程。 Check out that link and you can see how it might be done in the future (if the PR doesn't change). 查看该链接,您可以看到将来可能如何做(如果PR不变)。

For now, you would have to override the verify() method: 现在,您将不得不重写verify()方法:

User.prototype.verify = function(options, fn) {
  var user = this;

  // do a lot of audits and other stuff (check the current source)...

  // Set the token any way you like...
  user.verificationToken = "123456"; // <-- this probably isn't a good way
  user.save(function(err) {
    if (err) {
      fn(err);
    } else {
      sendEmail(user);
    }
  });

  function sendEmail(user) {
    // you should be able to keep this exactly as it is in the source...
  }
};

And of course, don't forget to read about verifying user email addresses in the LoopBack documentation! 当然,不要忘记阅读LoopBack文档中有关验证用户电子邮件地址的内容!

Another option is to pass the token generation function to the options object. 另一个选择是将令牌生成功能传递给options对象。 You can see how it's used if you scroll down a bit . 向下滚动一下,您可以看到它的用法

Check my answer to a somewhat related question for steps on how to override the User model and pass the options object to the verify() method. 检查我对一个相关问题的答案,以获取有关如何覆盖User模型并将options对象传递给verify()方法的步骤。

You can do this in the remote hook after you create a new User, eg: 创建新用户后,可以在远程挂钩中执行此操作,例如:

User.afterRemote('create', function(context, user, next) {
    var userModel = User.constructor;

    myTokenGenerator = function(user, cb) {
      myToken = '1234'
      cb(null, myToken);
    };

    var options = {
      type: 'email',
      mailer: Email,
      to: user.email,
      from: senderEmail,
      subject: 'My subject',
      template: path.resolve(__dirname, '../views/verify.ejs'),
      user: user,
      host: myEmailHost,
      port: myEmailPort,
      generateVerificationToken: myTokenGenerator
    };

    user.verify(options, function(err, response) {

      if (err) {
        next(err);
        return;
      }
      console.log("Account verification email sent to " + options.to);
      next();
      return;
    });
}); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM