Hi guys ive spent the last 3 hours trying to get this work so any help would be greatly appreciated. So i made an express webapp that has an admin page. I have the register and login done using passport and i can check if the user is logged in with the function
var isAuthenticated = function (req, res, next) {
// if user is authenticated in the session, call the next() to call the next request handler
// Passport adds this method to request object. A middleware is allowed to add properties to
// request and response objects
if (req.isAuthenticated())
return next();
// if the user is not authenticated then redirect him to the login page
res.redirect('/');}
I when i click on the link for my admin route i want to check to see if the users username is equal to example and if it is allow it into the page. My code so far is
var isAdmin = function (req, res, next) {
var post = req.body;
if (post.username === 'example') {
return next();
}else{
// if the user is not authenticated then redirect him to the login page
res.redirect('/');
}
and my route is
router.get('/admin', isAdmin, function(req, res){
res.render('admin', { title : 'admin' });
});
I have both of these code snipets in my index route file. The problem that i am getting is that when i try to log in with the user called example it returns me to the login page. Any ideas how to fix?
Aucun commentaire:
Enregistrer un commentaire