strongloop loopback:使用远程钩子扩展内置用户模型会导致无限循环

时间:2015-09-29 17:24:54

标签: model hook loopbackjs strongloop

我需要在环回应用程序中启用电子邮件注册确认。

关注以下文档: https://docs.strongloop.com/display/public/LB/Registering+users#Registeringusers-Verifyingemailaddresses

我还没有能够正确使用内置的用户模型,这里的文档不是很清楚。

所以我创建了一个名为CancellationTokenSource.CancelAfter(TimeSpan)的附加模型。

RegisteredUser

model-config.json中的相关部分:

//registereduser.js: this is mostly from the docs


var path = require('path');

  //send verification email after registration
module.exports = function(RegisteredUser) {
  RegisteredUser.observe('after save',function(ctx,next) {
    var user = ctx.instance;
    console.log('> user.afterRemote triggered');
    console.log(user);
    var options = { 
      type: 'email',
      to: user.email,
      from: 'noreply@cantoalegre.com.co',
      subject: 'Gracias por registrarte!',
      template: path.resolve(__dirname, '../../server/views/email_verify.html'),
      redirect: '/verified',
      user: user
    };  
    console.log("> before verify");
    user.verify(options, function(err, response) {
      if (err) {
        console.log("errrrrrrrrrrrr");
        console.log(err);
        next(err);
        return;
      }   
      console.log('> verification email sent:', response);
      ctx.res.render('response', {
        title: '¡Registración completada con éxito!',
        content: 'Por favor revisa tu correo y haz clic en el enlace que ahí encontrarás antes de usar la app',
        redirectTo: '/',
        redirectToLinkText: 'Ingresar'
      }); 
    }); 
    console.log("> after verify");
  }); 
}

//registereduser.json
{
  "name": "RegisteredUser",
  "base": "User",
  "idInjection": true,
  "properties": {
  },
  "validations": [],
  "relations": {
  },
  "acls": [
    {
      "accessType": "READ",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "DENY"
    },
    {
      "accessType": "WRITE",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "DENY"
    },
    {
      "accessType": "WRITE",
      "principalType": "ROLE",
      "principalId": "admin",
      "permission": "ALLOW"
    }
  ],
  "methods": {}
}

现在,发生的事情确实是后挂钩被触发,但显然它进入无限循环 - 好像它会立即登录并保持登录,因为 "RegisteredUser": { "dataSource": "my_ps_DS", "options": { "emailVerificationRequired": true } }, 似乎表现出不断变化令牌。服务器输出:

console.log(user)

我认为我在做错事之前认为它是环回中的错误还是什么?

1 个答案:

答案 0 :(得分:0)

瞧不起我。

after create而不是after save。但是,它还没有工作,还原到旧的符号RegisteredUser.afterRemote('create', function....