i'M使用我的app.js node.js后端的mongoclient connexion池,它运行良好:
var db;
MongoClient.connect("mongodb://localhost:27017", function(err, client) {
if (err) throw err;
db = client.db('myDb');
// Start the application after the database connection is ready
app.listen(3000);
console.log("Listening on port 3000");
});
app.post('/getEvaluations', function(req, res) {
db.collection('evaluations').find().toArray(function(err, docs) {
if (err) throw err;
res.send(docs);
});
})
我想构建HTML表单,以获取登录密码,但是之后如何使用新的登录密码更改Mongoclient连接?
例如,只要约翰登录,他就需要拥有阅读权才能致电getevaluations web服务。
我是否必须始终使用没有用户的连接,然后仅检查每个Web服务中的用户会话吗?
或每次使用MongoDB用户登录时(他可能只有阅读版权(时,可以更改池连接吗?
这样的东西?
MongoClient.connect("mongodb://john:passwd@localhost:27017", function(err, client) {
if (err) throw err;
db = client.db('myDb');
// Start the application after the database connection is ready
app.listen(3000);
console.log("Listening on port 3000");
});
例如,假设使用密码" passwd"要登录的"约翰",我该怎么办?使用连接池的某个地方是否有任何标准代码?
我也使用明确的课程,但不明白如何与连接池一起使用。
我找不到使用连接池的mongodb auth代码。有一些Moogoose示例,但不是相同的。
这是我有的想法:
用户通过HTTP登录,则如果登录还可以,则创建了会话,但是如果用户在所有会话中仅具有读取权限,我该如何检查?
这是一个伪代码,来自Moongoose的示例,但这不是我需要的,我正在使用Mongoclient。
app.get('/getAuth', function(req, res) {
var email = req.param('email');
var password = req.param('password');
//authenticate input against database
UserSchema.statics.authenticate = function (email, password, callback) {
User.findOne({ email: email })
.exec(function (err, user) {
if (err) {
return callback(err)
} else if (!user) {
var err = new Error('User not found.');
err.status = 401;
return callback(err);
}
bcrypt.compare(password, user.password, function (err, result) {
if (result === true) {
return callback(null, user);
} else {
return callback();
}
})
});
}
})
如果有人有示例代码,我真的很感激,我不使用Moogoose,但是猫头鹰谢谢。
或也许,此auth Web服务会更好:
app.get('/getAuth', function(req, res) {
var user = req.param('user');
var password = req.param('password');
db.authenticate(user, password, function(err, res) {
// callback
});
})
但是,我该如何处理连接池?我该如何重新加载?我不明白该怎么做。
编辑1:这是我现在得到的:我已经构建了一个getauth Web服务,其中有一个池连接开始,这是正确的方法吗?
var db;
app.get('/getAuth', function(req, res) {
var user = "jose";
var password = "passwd";
MongoClient.connect("mongodb://"+user+":"+password+"@localhost:27017", function(err, client) {
if (err) {console.log(err)}
else{
db = client.db('myDb');
// Start the application after the database connection is ready
console.log("correct log in");
}
});
})
我现在如何使用会话?
编辑2:似乎正在使用此文档使用Expressession的代码:https://codeforgeek.com/manage-session-using-node-js-express-4/
//use sessions for tracking logins
app.use(session({secret: 'ssshhhhh',saveUninitialized: true,resave: true}));
sess ={};
// ---------------------------------------------------------------------AUTH USER
app.get('/getAuth', function(req, res) {
var user = "jose";
var password = "passwd";
MongoClient.connect("mongodb://"+user+":"+password+"@localhost:27017", function(err, client) {
if (err) {console.log(err)}
else{
db = client.db('myDb');
sess = req.session;
/*
* Here we have assign the 'session' to 'sess'.
* Now we can create any number of session variable we want.
* in PHP we do as $_SESSION['var name'].
* Here we do like this.
*/
sess.email = "jose.golo@gmail.com"; // equivalent to $_SESSION['email'] in PHP.
sess.username = "jose"; // equivalent to $_SESSION['username'] in PHP.
res.end('done');
}
});
})
然后,在我的GetEvaluations Web服务中,我检查SESS是否包含一封电子邮件,其他,这意味着他没有登录,因此他无法访问评估Node.js Web服务:
app.post('/getEvaluations', function(req, res) {
if (!sess.hasOwnProperty("email") ){
console.log('not logged in ');
}else{
db.collection('evaluations').find().toArray(function(err, docs) {
if (err) throw err;
res.send(docs);
});
}
})
如果您有更好的解决方案,请告诉我。该应用是前端离子1应用。
问题:这是正常的行为:我在Firefox内得到了身份验证,因此我可以访问评估。现在,我在同一台计算机上转到Chrome,我不必进行身份验证即可查看评估?是因为它是基于用户ID的会话吗?
这就是我的方式:
- 让用户使用邮政方法通过HTTP表单登录。
- 尝试在Mongo数据库中找到用户的文档(例如,用户名(。这将导致一个用户对象包含用户的凭据以及他/她拥有的任何权利。
- 将凭据与登录表单提供的输入(包含在请求对象的正文中(。
- 如果给定的输入和以前存储的凭据相等,则可以创建一个会话并将用户对象分配给它。
- 当用户导航到"/geteValuations"时,请检查请求中是否有会话对象。此会话对象将包含当前登录的用户的用户对象。
- 仅当用户有适当的情况权利。
您可以尝试使用Passport.js等身份验证中间件。这些将为您处理会话及其用户数据对象。这是使用本地策略的示例:https://github.com/passport/express-4.x-local-example