如何检查数据库中是否存在用户名和密码(mongodb)

时间:2022-08-17 07:19:43

i want to store username and password inside mongodb database and later on retrieve database values and check whether the username and password exists inside database.If it does exist then i would redirect to another page.How can i achieve this using node.js and mongodb.I am able to store the values inside database.But getting confused for how to fetch the values and check them against the values provide in form field values.There is no method in mongodb like fetchByName or something similar. Can someone help me out with the code.

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

2 个解决方案

#1


3  

I think you should take a look on the Nodepad source code, it explains very well how to achieve this with Mongoose:

我认为你应该看一下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


1  

try using an already existing library like passport or everyauth. There are other ones too, google them :)

尝试使用现有的图书馆,如passport或everyauth。还有其他的,谷歌:)

#1


3  

I think you should take a look on the Nodepad source code, it explains very well how to achieve this with Mongoose:

我认为你应该看一下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


1  

try using an already existing library like passport or everyauth. There are other ones too, google them :)

尝试使用现有的图书馆,如passport或everyauth。还有其他的,谷歌:)