环回:重置后未调用事件 resetPasswordRequest

Loopback: Event resetPasswordRequest not called after reset

我正在尝试进入 Loopback 上的密码重置过程,这样我就可以向用户发送一封包含说明的电子邮件。我创建了一个名为 'user' 的自定义模型,它扩展了 'User'。然后我添加了 "User.on('resetPasswordRequest'...",但它永远不会被执行。请检查 user.js

上的 console.log

型号-config.json

{
...

  "user": {
    "dataSource": "mysqlDs",
    "public": true,
    "options": {
      "emailVerificationRequired": false
    }
  },
...
}

user.js上

{
  "name": "user",
  "base": "User",
  "idInjection": true,
  "properties": {},
  "validations": [],
  "relations": {},
  "acls": [
    {
      "principalType": "ROLE",
      "principalId": "$everyone",
      "accessType": "READ",
      "permission": "ALLOW"
    }
  ],
  "methods": []
}

user.js

module.exports = function(User) {

  console.log('It prints this log here.');

  User.on('resetPasswordRequest', function(info) {

    console.log('But it does not print this log here ever.');

    var url = 'http://www.example.com/reset-password';
    var html = 'Click <a href="' + url + '?access_token=' + info.accessToken.id + '">here</a>';
    loopback.Email.send({
      to: info.email,
      from: 'mail@example.com',
      subject: 'My Subject',
      html: html
    }, function() {
      console.log('> sending password reset email to:', info.email);
      if (err) return console.log('> error sending password reset email');
    });
  });

};

难道你只是想念定义一个远程方法来调用你的代码吗? 这就是我要做的:

user.js

    module.exports = function(User) {

      console.log('It prints this log here.');

      User.resetPasswordRequest = function (info, cb) {

        console.log('But it does not print this log here ever.');

        var url = 'http://www.example.com/reset-password';
        var html = 'Click <a href="' + url + '?access_token=' + info.accessToken + '">here</a>';
        loopback.Email.send({
          to: info.email,
          from: 'mail@example.com',
          subject: 'My Subject',
          html: html
        }, function() {
          console.log('> sending password reset email to:', info.email);
          if (err) {
             cb(err, false);
             return console.log('> error sending password reset email');
          }
          cb(err, true):
        });
      });

    User.remoteMethod('resetPasswordRequest', {
            description: 'Send a mail to an User to permit him to reset his password',
            accepts: {arg: 'info', type: 'object', required: true},
            returns: {arg: 'mailSend', type: 'boolean'},
            http: {verb: 'post'}
        });

};

然后,使用资源管理器或通过 REST 调用 API,同时发布您的 info json,例如:

{
 accessToken: xxx,
 email: xxx
}

并且不要忘记在 user.js 中更新您的 ACL,以便您可以从远程调用该方法:

{
  "name": "user",
  "base": "User",
  "idInjection": true,
  "properties": {},
  "validations": [],
  "relations": {},
  "acls": [
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "DENY"
    },
    {
      "accessType": "READ",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW"
    },
    {
      "accessType": "WRITE",
      "principalType": "ROLE",
      "principalId": "$authenticated",
      "permission": "ALLOW"
    },
    {
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW",
      "property": "resetPasswordRequest"
    }
  ],
  "methods": []
}

解决方法在这里解释: http://loopback.io/doc/en/lb3/Extending-built-in-models.html#setting-up-a-custom-model

所以你需要:

User.setup = function() {
  var User = this;
  // since setup is called for every extended model
  // the extended model will also have the event listener
  User.on('resetPasswordRequest', function() {
    ///Do your stuff here
  });
}

好的,@IvanSschwarz 是对的。太感谢了!我的初始代码是正确的,但缺少模型上的一个小改动-config.json:出于某种原因我需要隐藏用户模型。所以我也借此机会将模型名称从 "user" 更改为 "member",以遵循环回良好实践指南。

   ...
   "User": {
     "dataSource": "mysqlDs",
     "public": false
   },
   "member": {
     "dataSource": "mysqlDs",
     "public": true,
     "options": {
       "emailVerificationRequired": true
     }
   },
   ....

我相信其他建议也可能有效,但我想通过 JSON 扩展用户模型,并利用用户模型中的内置远程方法重置密码。所以谢谢你们!