使用Loopback实现更改密码

Vic*_*ves 10 javascript node.js strongloop loopbackjs

我正在尝试使用Loopback的内置方法实现更改密码功能,它工作正常,但它不会更新密码,hash而只是在db中保存纯文本.我loopback-component-passport在这个项目中使用npm包.我搜索了很多网站,但我无法找到实现此功能的正确方法.有谁知道如何做到这一点?

//Change user's pasword
app.post('/change-password', function(req, res, next) {
  var User = app.models.user;
  if (!req.accessToken) return res.sendStatus(401);
  //verify passwords match
  if (!req.body.password || !req.body.confirmation ||
    req.body.password !== req.body.confirmation) {
    return res.sendStatus(400, new Error('Passwords do not match'));
  }

  User.findById(req.accessToken.userId, function(err, user) {
    if (err) return res.sendStatus(404);
    user.hasPassword(req.body.oldPassword, function(err, isMatch) {
      if (!isMatch) {
        return res.sendStatus(401);
      } else {
        user.updateAttribute('password', req.body.password, function(err, user) {
          if (err) return res.sendStatus(404);
          console.log('> password change request processed successfully');
          res.status(200).json({msg: 'password change request processed successfully'});
        });
      }
    });
  });
});
Run Code Online (Sandbox Code Playgroud)

Med*_*uly 13

使用内置的User.hashPassword源代码

//Hash the plain password
user.updateAttribute('password', User.hashPassword(req.body.password), function(err, user) {
    ...
});
Run Code Online (Sandbox Code Playgroud)

  • @VickyGonsalves你是受欢迎的,当你无法弄清楚环回的东西,总是检查源代码,它有助于很多 (2认同)