4

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?

Prashant Tapase
  • 2,132
  • 2
  • 25
  • 34
INFOSYS
  • 1,465
  • 9
  • 23
  • 50

1 Answers1

6

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;
  };
geek_guy
  • 607
  • 1
  • 5
  • 17