在 Angular-Strongloop 应用程序中更改密码的正确方法是什么?

What is the correct way to change password in Angular-Strongloop app?

我查看了文档、SO 和互联网,无法找到一个示例来说明如何使用 Strongloop 后端和使用 AngularJS API 由 slc 生成。高度赞赏正确方向的指示。

更新 我不是在寻找重置密码,因为密码忘记了。 我正在寻找一种合法的方式来更改需要提供旧通行证和新通行证的位置,并且在设置新通行证之前将验证旧通行证。

无需再次登录用户,只需运行 User 上的 hasPassword 方法来比较密码,如果为真,则更新属性

像这样... if(user.hasPassword(req.body.oldPassword)) { user.updateAttribute('password', req.body.password .... }

https://apidocs.strongloop.com/loopback/#user-prototype-haspassword

假设应用程序用户模型被称为 MyUserModel 并继承自内置模型 User

您有两个选择:

  1. 如果只能在AngularJS/客户端工作

    在 MyUserModel 上使用 update 远程方法(PUT 请求)并仅指定

    { "password":"...newpassword..."}

    在体内。

    但是,为了对新密码实施安全策略,使用特定的远程方法可能比此技巧更方便。

  2. 如果你可以在 NodeJS / LoopBack 服务器端工作。

    这是我的 "full" 解决方案,用于在 LoopBack / StrongLoop - IBM 项目中实现特定的 updatePassword 远程方法。 请确认 loopback-datasource-juggler 包的版本高于或等于 2.45.1 (npm list loopback-datasource-juggler)。

"my-user-model.js"

module.exports = function (MyUserModel) {

...

MyUserModel.updatePassword = function (ctx, emailVerify, oldPassword, newPassword, cb) {
  var newErrMsg, newErr;
  try {
    this.findOne({where: {id: ctx.req.accessToken.userId, email: emailVerify}}, function (err, user) {
      if (err) {
        cb(err);
      } else if (!user) {
        newErrMsg = "No match between provided current logged user and email";
        newErr = new Error(newErrMsg);
        newErr.statusCode = 401;
        newErr.code = 'LOGIN_FAILED_EMAIL';
        cb(newErr);
      } else {
        user.hasPassword(oldPassword, function (err, isMatch) {
          if (isMatch) {

            // TODO ...further verifications should be done here (e.g. non-empty new password, complex enough password etc.)...

            user.updateAttributes({'password': newPassword}, function (err, instance) {
              if (err) {
                cb(err);
              } else {
                cb(null, true);
              }
            });
          } else {
            newErrMsg = 'User specified wrong current password !';
            newErr = new Error(newErrMsg);
            newErr.statusCode = 401;
            newErr.code = 'LOGIN_FAILED_PWD';
            return cb(newErr);
          }
        });
      }
    });
  } catch (err) {
    logger.error(err);
    cb(err);
  }
};

MyUserModel.remoteMethod(
  'updatePassword',
  {
    description: "Allows a logged user to change his/her password.",
    http: {verb: 'put'},
    accepts: [
      {arg: 'ctx', type: 'object', http: {source: 'context'}},
      {arg: 'emailVerify', type: 'string', required: true, description: "The user email, just for verification"},
      {arg: 'oldPassword', type: 'string', required: true, description: "The user old password"},
      {arg: 'newPassword', type: 'string', required: true, description: "The user NEW password"}
    ],
    returns: {arg: 'passwordChange', type: 'boolean'}
  }
);

...
};

"my-user-model.json"

{
   "name": "MyUserModel",
   "base": "User",

   ...

   "acls": [
     ...
     {
       "comment":"allow authenticated users to change their password",
       "accessType": "EXECUTE",
       "property":"updatePassword",
       "principalType": "ROLE",
       "principalId": "$authenticated",
       "permission": "ALLOW"
     }
   ...
   ],
   ...
}