使用其余端点 POST /users/{id}/verify 重新验证电子邮件

Reverification of email using the rest end point POST /users/{id}/verify

当用户在创建用户后没有收到验证电子邮件时,我正在尝试发送重新验证电子邮件。下面是配置:

Users.getVerifyOptions = function() {
  const defaultOptions = {
  type: 'email',
  from: 'no-reply@example.com',
  subject: 'Re-verification Email',
  template: path.resolve(__dirname, '../../server/views/verify.ejs'),
  redirect: '',
  host: "mywebsite.com",
  port: 80
  };
return Object.assign({}, this.settings.verifyOptions || defaultOptions);
};

//Re-verify Method to render
Users.afterRemote('prototype.verify', function(context, user, next) {
  context.res.render('response', {
title: 'A Link to reverify your identity has been sent '+
  'to your email successfully',
content: 'Please check your email and click on the verification link '+
  'before logging in',
redirectTo: '',
redirectToLinkText: ''
  });
 });

现在的问题是,如果用户已经通过验证,并且数据库中的选项 emailVerified 设置为 true,那么对 POST /users/{id}/verify 的请求也会生成一个新的验证令牌并发送一个电子邮件。如果 emailverified 选项设置为 true 并且 return 一条消息说电子邮件已经验证,是否有任何方法可以阻止此端点发送任何电子邮件?

You can try by check user.emailVerified is true or false

示例:

Users.afterRemote('prototype.verify', function (context, user, next) {
  if(!user.emailVerified) {
    context.res.render('response', {
      title: 'A Link to reverify your identity has been sent ' +
        'to your email successfully',
      content: 'Please check your email and click on the verification link ' +
        'before logging in',
      redirectTo: '',
      redirectToLinkText: ''
    });
  } else {
    return next('already verified');
  }
});

Or else you can check verified user when login, when not verified send verification link.

示例:

app.post('/login', function(req, res) {
  User.login({
    email: req.body.email,
    password: req.body.password
  }, 'user', function(err, token) {
    if (err) {
      if(err.details && err.code === 'LOGIN_FAILED_EMAIL_NOT_VERIFIED'){
        //send verification link
      }
    }
  })
});