node.js - mongodb 并在 node.js 中进行身份验证和通行证

标签 node.js mongodb authentication passport.js

我在 mongodb 中有一个这样的集合

{
   username:silver,
   Email:sil@gmail.com,
   password:silvester,
}

为了进行身份验证,我将从数据库中获取数据,然后我将使用这样的 if 语句检查给定的电子邮件是否存在

app.post("/login",function(req,res){

   var email=req.body['emailid'];

   collection.find({email:sil@gmail.com}).toArray(function(err,res)
   {
      if(res.length==0){
         console.log("name is not exist");
      }else{
         if(res.email==email){
            console.log("email is exist");
         }else{
            console.log("not exist");
         }
      }
   });
});

所以在这里如何使用 Passport 模块进行身份验证。让我通过配置示例代码了解它。 我正在使用 express3.x 框架。那么如何配置它。

最佳答案

Here您可以阅读本地策略,以及 here关于配置。

您的本地策略应如下所示:

passport.use(new LocalStrategy({
        emailField: 'email',
        passwordField: 'passw',
    },

    function (emailField, passwordField, done) {
        process.nextTick(function () {
            db.collection(dbCollection, function (error, collection) {
                if (!error) {
                    collection.findOne({
                        'email': sil@gmail.com
                        'password': silvester // use there some crypto function
                    }, function (err, user) {
                        if (err) {
                            return done(err);
                        }
                        if (!user) {
                            console.log('this email does not exist');
                            return done(null, false);
                        }
                        return done(null, user);
                    });
                } else {
                    console.log(5, 'DB error');
                }
            });
        });
    }));

关于node.js - mongodb 并在 node.js 中进行身份验证和通行证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15286824/

相关文章:

c - 打开 SSL 错误 : implicit declaration of MD5Init

node.js - PUGJS - 如何访问本地人的数据?

node.js - Node 提供的 ejs 文件可以使用离线 Bootstrap 吗?

node.js - 为什么 Mongo 给这个字段分配一个 id?

javascript - Koa Auth 流程与 compose

php - 命令行脚本中无法识别 MongoDB PHP 扩展

java - 多个 Tomcat validator 阀

mongodb不可能(?)E11000重复键错误dup key当upserting

python - 通过 httpd 上的 Flask-PyMongo 连接到 MongoHQ URI 时出现 ConnectionFailure

ruby-on-rails - 测试 Rails 应用程序在没有登录的情况下无法访问