我只有一个主页,如果用户成功登录,则登录页面应将其重定向到仪表板。这部分工作正常 但是如果我尝试打开http://localhost:9090/dashboard之类的链接,它就会打开。我希望如果用户未登录,他将永远无法打开仪表板页面
module.exports = function(app, passport){
app.get('/', function(req, res){
res.render('index.ejs');
});
app.get('/login', function(req, res){
res.render('login.ejs', { message: req.flash('loginMessage') });
});
app.post('/login', passport.authenticate('local-login', {
successRedirect: '/dashboard',
failureRedirect: '/login',
failureFlash: true
}));
app.get('/signup', function(req, res){
res.render('signup.ejs', { message: req.flash('signupMessage') });
});
app.post('/signup', passport.authenticate('local-signup', {
successRedirect: '/',
failureRedirect: '/signup',
failureFlash: true
}));
app.get('/dashboard',isLoggedIn, function (req,res) {
res.render('dashboard.ejs');
} );
app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
})
};
function isLoggedIn(req, res, next) {
if(req.isAuthenticated()){
return next();
}
res.redirect('/login');
}
答案 0 :(得分:0)
问题在于您的路由呼叫,请检查路由注销
<ul class="nav navbar-nav -align-left">
<li class="active"><a href="#">Home</a></li>
<li><a href="/history">Last 15 Search</a></li>
<li><a href="/logout">Logout</a></li> </ul>