How to override the default password hashing method and validation method of loopback?

青春壹個敷衍的年華 提交于 2019-12-10 18:23:51

问题


I am very new to Loopback and i want to override the default hashing of password method of loopback to the one that is currently used in my backend so that i can sync this application with that database .

i read this link https://groups.google.com/forum/#!topic/loopbackjs/ROv5nQAcNfM but i am not able to understand how to override the password and validation?


回答1:


You can override User.hashPassword to implement your own hashing method.

Let's say you have a model Customer which has User as base model,

Inside customer.js, you can add follwoinng snippet,

Customer.hashPassword = function(plain) {
    this.validatePassword(plain);
    return plain + '1'; // your hashing algo will come here.
  };

This will append 1 to all the passwords.

Now you need to override another function User.prototype.hasPassword which matches the user input password with the hashed password,

Customer.prototype.hasPassword = function(plain, fn) {
    fn = fn || utils.createPromiseCallback();
    if (this.password && plain) {
      const isMatch = this.password === plain + '1' // same hashing algo to come here.
      fn(null, isMatch)
    } else {
      fn(null, false);
    }
    return fn.promise;
  };


来源:https://stackoverflow.com/questions/46089049/how-to-override-the-default-password-hashing-method-and-validation-method-of-loo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!