NodeJS – 显示login或不是用户的不同内容
我试图显示不同的内容login,而不是在一个页面上的用户。
这是我用来生成/页面的代码:
app.get('/',function(req, res){ if (!checkSession(req, res)) { res.render('index.ejs', { title: 'FrontSpeak - blog-based social network' }) } else { res.render('index.ejs', { title: 'autrhorized' }) } })
checkSession函数:
function checkSession(req, res) { if (req.session.user_id) { db.collection('users', function (err, collection) { collection.findOne({ _id: new ObjectID(req.session.user_id) }, function (err, user) { if (user) { req.currentUser = user; return true; } else { return false; } }); }); } else { return false; } }
loginfunction:
app.post('/', function(req, res){ db.collection("users", function (err, collection) { collection.findOne({ username: req.body.username }, function (err, doc) { if (doc && doc.password == req.body.password) { console.log("user found"); req.session.user_id = doc._id; } } }); }); });
所以,它似乎并没有工作。 不过,我认为这不是显示不同内容的最佳方式。 可能有一些更优雅的方法来做到这一点? 谢谢!
更新:新的loginfunction:
app.post('/', function(req, res){ db.collection("users", function (err, collection) { collection.findOne({ username: req.body.username }, function (err, doc) { console.log('found user'); if (doc && doc.password == req.body.password) { req.session.user_id = doc._id; res.redirect('/'); }; res.redirect('/'); }); res.redirect('/'); }); });
这是尝试将传统的同步模型应用于Node的asynchronouscallback驱动模型的一种情况。
数据库查询完成后,您返回true
,但您只是返回到数据库驱动程序。 checkSession
很久以前就返回了。 由于该函数返回未定义,如果有一个session.user_id
(和假如没有),login检查将始终评估为false。
相反,你可以使用Brandon的build议使checkSession
asynchronous,或者我推荐实现一个中间件function:
function checkLogin(req, res, next) { if (req.session.user_id) { db.collection('users', function (err, collection) { if (err) return next(err); // handle errors! collection.findOne({ _id: new ObjectID(req.session.user_id) }, function (err, user) { if (user) { req.currentUser = user; } else { req.currentUser = null; } next(); }); }); } else { req.currentUser = null; next(); } }
现在有两种使用中间件function的方法。 如果你想检查每个请求的用户,只需将其添加到应用程序:
app.use(checkLogin);
现在每一个请求都会有一个req.currentUser
,但是每次请求都会导致从数据库中获取login状态的性能。 或者,如果您只需要某些请求的用户信息,请将该function粘贴在路由中:
app.get('/', checkLogin, function(req, res) { if (req.currentUser) { // logged in } else { // not } });
您可以在快速文档中阅读更多关于此的信息 。
它看起来像你试图通过检查它的返回值来使用checkSession
作为一个同步函数,但checkSession
不能同步,因为它依赖于asynchronousfunction,即callback在这里: db.collection('users', function (err, collection) ...
你需要修改checkSession
是asynchronous的:
function checkSession(req, res, callback) { if (req.session.user_id) { db.collection('users', function (err, collection) { collection.findOne({ _id: new ObjectID(req.session.user_id) }, function (err, user) { if (user) { req.currentUser = user; callback(true); } else { callback(false); } }); }); } else { callback(false); } }
然后在请求处理程序中asynchronous使用它:
app.get('/',function(req, res){ checkSession(req, res, function(isUser) { if (!isUser) { res.render('index.ejs', { title: 'FrontSpeak - blog-based social network' }) } else { res.render('index.ejs', { title: 'autrhorized' }) } }); })
- 尝试在节点中实现JWT身份validation。 在受保护的路线上获得未经授权
- 在对Google表单进行多次调用时,API身份validation不适用于第二次调用:请求的身份validation范围不足
- 使用Passport基于dynamic路由进行身份validation
- Passport.js + Express.jsauthentication后将用户转发到原来的目的地
- Nodemailer不断拒绝无效login身份validation(不使用Gmail)
- 使用JSON Web令牌和node.js / express保护URL的正确方法
- 检测提供商注销passport.js?
- node.js connect-auth应用程序? 例子(用户注册/用户/会话pipe理)
- 使用express-jwt时,为什么我的授权标题在显式存在的时候丢失了