如何覆盖默认的密码散列方法和环回验证方法?

INF*_*SYS 4 loopback node.js loopbackjs

我对环回非常陌生,我想将环回的密码方法的默认散列覆盖为我后端当前使用的方法,以便我可以将此应用程序与该数据库同步。

我阅读了此链接https://groups.google.com/forum/#!topic/loopbackjs/ROv5nQAcNfM但我无法理解如何覆盖密码和验证?

gee*_*guy 6

您可以重写User.hashPassword以实现您自己的散列方法。

假设您有一个以 User 作为基本模型的模型 Customer,

在 customer.js 中,您可以添加以下代码段,

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

这会将 1 附加到所有密码。

现在您需要覆盖另一个User.prototype.hasPassword将用户输入密码与散列密码匹配的函数,

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;
  };
Run Code Online (Sandbox Code Playgroud)