如何覆盖默认的密码哈希方法和环回验证方法? [英] How to override the default password hashing method and validation method of loopback?

查看:93
本文介绍了如何覆盖默认的密码哈希方法和环回验证方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Loopback还是很陌生,我想将默认的loopback密码哈希方法覆盖到后端当前使用的方法,以便我可以将此应用程序与该数据库同步.

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 .

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

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?

推荐答案

您可以覆盖User.hashPassword来实现自己的哈希方法.

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,

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

Inside customer.js, you can add follwoinng snippet,

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

这会将1附加到所有密码上.

This will append 1 to all the passwords.

现在,您需要覆盖另一个功能User.prototype.hasPassword,该功能将用户输入的密码与哈希密码相匹配,

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;
  };

这篇关于如何覆盖默认的密码哈希方法和环回验证方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆