【问题标题】:What is the correct way to change password in Angular-Strongloop app?在 Angular-Strongloop 应用程序中更改密码的正确方法是什么?
【发布时间】:2016-06-09 18:19:38
【问题描述】:

我浏览了文档、SO 和互联网,找不到显示如何使用 Strongloop 后端和使用 slc 生成的 AngularJS API 的应用程序更改用户密码的示例。高度赞赏正确方向的指针。

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

【问题讨论】:

  • 快速搜索 StrongLoop API 文档带来apidocs.strongloop.com/loopback/#user-resetpassword
  • @Claies 那不是我要找的,重置密码是在忘记密码的情况下。我正在寻找一个合法的更改,其中需要提供旧通行证和新通行证,并且在设置新通行证之前将验证旧通行证。我将这个添加到问题中是为了让人们更容易。
  • ok,所以根据the official sample source,你可以使用user.updateAttribute('password', req.body.password, function(err, user)
  • @Claies 这仍然是重置密码的示例,这不是验证密码更改的安全方式。这不是更改密码的标准方法。修改密码的用户应该知道旧密码。
  • 所以您只需在表单上添加当前密码确认字段,并在调用更新之前验证密码。如果不为您编写模块,我不知道如何回答您的问题。

标签: angularjs strongloop


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

    你有两个选择:

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

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

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

      在体内。

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

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

      这是我在 LoopBack / StrongLoop - IBM 项目中实现特定 updatePassword 远程方法的“完整”解决方案。 请验证loopback-datasource-juggler 包的版本是否高于或等于 2.45.1 (npm list loopback-datasource-juggler)。

    “我的用户模型.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'}
      }
    );
    
    ...
    };
    

    “我的用户模型.json”

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-17
      • 2019-09-08
      • 1970-01-01
      • 2010-11-10
      • 1970-01-01
      • 2021-05-24
      • 1970-01-01
      相关资源
      最近更新 更多