问题
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