我正在使用nodejs,express,mongoose和护照npm制作webapp。nodejs护照没有注销,因为中间件
之前我的登录和注销功能工作正常,但现在我不得不添加一项功能来检查用户是高级用户还是普通用户。 所以我做了一个中间件,它现在导致注销问题。 这是我的代码。
在我的index.js文件中,每秒调用一次/ matchData上的ajax请求以根据用户类型获取数据。
//ajax routes for json response
router.get('/matchData', isPremium);
//middleware causing problem
function isPremium(req,res,next){
if(req.isAuthenticated()){
if(req.user.type=='premium'){
console.log("user is premium");
Match.getMatchLatestData(function(err,match){
if(err) throw err;
res.send(match[0]);
});
}else{
console.log("user is loggedin but not premium");
Match.getMatchData(function(err,match){
if(err) throw err;
res.send(match[0]);
});
}
}else{
console.log("user not logged in");
Match.getMatchData(function(err,match){
if(err) throw err;
res.send(match[0]);
});
}
}
,在我的user.js的在我的登录和注销functionalty是如下
//Login Routes
router.get('/login',isAuthenticated2, function(req, res, next) {
res.render('user/login',{
title:'login'
});
});
router.post('/login',isAuthenticated2, passport.authenticate('local',{failureRedirect:'/users/login', failureFlash:'Invalid Email Id or Password'}),function(req,res){
console.log('Authentication Successful');
req.flash('success','You are logged in.');
res.redirect('/');
});
//logout route
router.get('/logout',isAuthenticated,function(req,res){
req.logout();
req.flash('success','You have logged out');
res.redirect('/');
});
function isAuthenticated(req, res, next) {
if(req.isAuthenticated())
return next();
else{
req.flash('error','Please Login First')
res.redirect('/users/login');
}
}
function isAuthenticated2(req, res, next) {
if(req.isAuthenticated()){
req.flash('info','You are already logged in.')
res.redirect('/');
}
else
return next();
}
现在的问题是,当我注销它显示闪存味精成功,但在我的控制台它仍然显示我登录而不是溢价或我溢价,这是,如果我在我登录的唯一情况。
感谢这解决了这个问题,也是另一个问题,即后退按钮后退出是让我登录。 –
一件事如何使用Flash的消息吧。现在闪存味精正在导致错误req.flash需要会话,虽然我的快车应用程序正在使用快速会话。 @Urvish –
1.注销后的后退按钮让我登录 - 每个页面上必须有一个终点,您检查用户是否登录。如果用户没有登录,那么它应该返回401或一些修复代码。在客户端代码中写入一个响应拦截器,如果任何响应返回401,则将其重定向到登录页面。 –