我正在一个网站上提供在线讲座。该网站有一个用户仪表板,该仪表板需要登录,还需要注册用户必须登录的课程。我有一个中间件设置,可以检查用户是否登录,效果很好。现在,我正在使用“ connect-flash”显示消息“请登录!”如果用户尝试注册课程或不登录而直接进入仪表板路线。在我的情况下,问题是尽管注册路线和仪表板路线都使用相同的中间件来检查用户是否已登录或否,如果用户未登录,则重定向到登录页面,我的Flash消息“请登录!”仅适用于仪表板路线,不适用于课程注册路线。
我正在添加相关代码和中间件功能。任何建议,将不胜感激。谢谢
添加所需的库
var express =require("express"),
app =express(),
mongoose =require('mongoose'),
passport =require('passport'),
LocalStrategy =require("passport-local"),
methodOverride =require('method-override'),
flash =require('connect-flash'),
passportLocalMongoose =require("passport-local-mongoose"),
bodyParser =require("body-parser");
导入必要的MongoDB模型。(尽管我认为这里没有问题)
var User =require('./models/user');
var CourseCompletion =require('./models/coursecompletionmodel.js');
var coursedetailmodel =require("./models/coursedetailmodel.js");
设置护照和连接闪存模块到应用程序
app.use(require("express-session")({
secret: "sunjeetjena1995",
resave: false,
saveUninitialized: false
}));
app.use(flash());
mongoose.connect("mongodb://localhost/DeepEigen_database")
app.use(bodyParser.urlencoded({extended: true}));
app.set("view engine", "ejs");
app.use(express.static(__dirname + '/public'));
app.use(methodOverride('_method'));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate({username:"username",password:"password"})));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use(function(req,res,next){
res.locals.currentUser=req.user;
res.locals.error=req.flash('error');
next();
});
.get路由到仪表板
app.get('/dashboard', isLoggedIn,function(req, res){
var id=req.user._id;
User.findById(id).populate("courses_registered_details").populate("course_completion").exec(function(err,founduser){
if(err){
console.log('A user failed to redirect to the dashboard');
console.log(err);
}
else{
res.render('dashboard', {user:founduser});
}
})
});
。获取注册课程的路线
app.get('/:id/machinelearningcoursedetails', isLoggedIn, function(req,res){
var id=(req.params.id);
console.log(id);
CourseCompletion.create({user_name:req.user.username,name:'Machine Learning',completed_numbers:0, inweek:0, total:10}, function(err, newccmodel){
if(err){
console.log(err);
}
else{
User.findOneAndUpdate(id,{$push:{courses_registered:{course:"Machine Learning", date:Date.now()},
course_completion:newccmodel}}, function(err, user){
if (err) {
console.log("Something wrong when updating data!");
console.log(err);
}
else{
coursedetailmodel.findOne({coursename:'Machine Learning'}, function(err, coursedetail){
if(err){
console.log('There is an error is adding the course detail data to the user object');
}
else{
User.findOneAndUpdate(id, {$push: {courses_registered_details:coursedetail}}, function(err, user){
if(err){
console.log('There was an error in adding course details object to User Object in Machine Learning');
}
else{
console.log('A user Successfully Registered for the Machine Learning Course');
console.log(req.user.username, req.user.email, req.user.profession);
}
})
}
});
}
});
}
});
res.redirect( '/dashboard');
});
转到登录页面
app.get('/login', function(req, res){
console.log(res.locals.error);
res.render('login');
});
用于检查是否已登录的中间件
function isLoggedIn(req,res,next){
if(req.isAuthenticated()){
return next();
}
else{
req.flash('error','Please Login!');
res.redirect("/login");
}
};