【发布时间】:2019-06-29 18:45:32
【问题描述】:
我正在创建一个简单的passport-local 登录应用程序。
在我成功登录之前,一切似乎都运行良好。
具体来说,我需要访问只有在成功登录后才能看到的/admin 视图。
这是我的 routes/index.js 文件:
module.exports = function(app, passport) {
// GET The HOME-page
app.get('/', function(req, res, next) {
res.render('index', {
title: 'Index',
message: 'Index Page'
});
});
// GET The LOGIN-page
app.get('/login', function(req, res, next) {
res.render('login', {
title: 'Login',
message: 'Login Page',
flash_message: req.flash('loginMessage')
});
});
// Process the login form
app.post('/login', passport.authenticate('local', {
successRedirect: '/admin',
failureRedirect: '/login',
failureFlash: true
}),
function (req, res) {
console.log('Hi');
res.redirect('/');
});
app.get('/admin', isLoggedIn, function (req, res) {
res.render('/admin', { <-ERROR IS POINTED HERE
title: 'Admin',
message: 'Admin page'
});
});
app.get('/logout', function (req, res) {
req.logout();
res.redirect('/');
});
};
// route middleware
function isLoggedIn(req, res, next) {
if (req.isAuthenticated()) { return next(); } <-ERROR IS POINTED HERE
res.status(403).send('[403] Forbidden');
}
我 console.logged req.isAuthenticated 省略了什么,它省略了 true,所以它应该返回 next()。当然,该页面会给出 500 代码,因此不会调用 403。
我将管理路径更改为没有isLoggedIn 的另一个页面,它可以正常工作,因此我的文件夹结构设置正确。
因此 isLoggedIn 函数搞砸了?但是如何,为什么会说 Failed to lookup,我不太明白。
【问题讨论】:
-
尝试在您的路线之前放置
isLoggedIn函数。在您的// GET The HOME-page之前 -
仍然存在同样的错误,我不认为js像C#那样线性编译代码。
标签: express routing passport.js