Answer the question
In order to leave comments, you need to log in
Passport js and express js. How to create roles?
Hello! There is a mongoose schema like this:
var mongoose = require('mongoose');
module.exports = mongoose.model('Admin',{
username: String,
password: String,
role: { type: String, default: "user" },
soft_token: { type: String, default: 0 }
});
Answer the question
In order to leave comments, you need to log in
Decided like this:
var isAuthenticated = function (req, res, next) {
var currentUserId = req.user ? req.user.id : false;
if(!currentUserId){
res.redirect('/');
return;
}
Admin.findById(currentUserId,function (err, user) {
if(!user || user.role !== "admin"){
res.redirect('/');
}else{
next();
}
})
}
router.get('/admin', isAuthenticated, function(req, res, next) {
res.end();
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question