【问题标题】:how to check whether username and password exists inside database(mongodb)如何检查数据库中是否存在用户名和密码(mongodb)
【发布时间】:2011-11-11 06:35:38
【问题描述】:

我想将用户名和密码存储在 mongodb 数据库中,稍后检索数据库值并检查用户名和密码是否存在于数据库中。如果确实存在,那么我将重定向到另一个页面。我如何使用 node.js 来实现这一点。 js和mongodb。我能够将值存储在数据库中。但是对于如何获取值并根据表单字段值中提供的值检查它们感到困惑。在mongodb中没有像fetchByName或类似的方法。 有人可以帮我写代码吗?

【问题讨论】:

    标签: mongodb node.js express mongoose


    【解决方案1】:

    我认为你应该看看Nodepad 源代码,它很好地解释了如何使用 Mongoose 实现这一点:

      User.virtual('password')
        .set(function(password) {
          this._password = password;
          this.salt = this.makeSalt();
          this.hashed_password = this.encryptPassword(password);
        })
        .get(function() { return this._password; });
    
      User.method('authenticate', function(plainText) {
        return this.encryptPassword(plainText) === this.hashed_password;
      });
    
      User.method('makeSalt', function() {
        return Math.round((new Date().valueOf() * Math.random())) + '';
      });
    
      User.method('encryptPassword', function(password) {
        return crypto.createHmac('sha1', this.salt).update(password).digest('hex');
      });
    
      User.pre('save', function(next) {
        if (!validatePresenceOf(this.password)) {
          next(new Error('Invalid password'));
        } else {
          next();
        }
      });
    

    【讨论】:

      【解决方案2】:

      尝试使用现有的库,例如 passporteveryauth。还有其他的,谷歌他们:)

      【讨论】:

        猜你喜欢
        • 2017-08-31
        • 1970-01-01
        • 2018-03-30
        • 2014-12-01
        • 1970-01-01
        • 2020-07-03
        • 1970-01-01
        • 2021-12-28
        • 1970-01-01
        相关资源
        最近更新 更多