express - Node.js middleware authentication with variable -


before route , render website want check if user authenticated , of type. "middleware" looks this:

function isauthenticated(req,res,next,required_type){ if(parse.user.current()){     parse.user.current().fetch().then(function(fetcheduser){         var type = fetcheduser.get("type");                     if(type == required_type){             return next();         }else{             res.redirect("/login");         }      }, function(error){         res.redirect("/login");     }); }else{     res.redirect("/login"); } } 

this part seems fair , easy enough. here how try use middleware before route:

app.get('/dashboard_client',isauthenticated, dashboard_client_view_controller.displayview); 

my questions is, how set required_type variable, since node.js somehow know how , find req,res , next variables. thank answering such stupidly , guess obvious solve question.

you try approach:

//middleware:

function isauthenticated(required_type) {     return function(req, res, next) {         if (parse.user.current()) {             parse.user.current().fetch().then(function (fetcheduser) {                 var type = fetcheduser.get("type");                 if (type == required_type) {                     return next();                 } else {                     res.redirect("/login");                 }              }, function (error) {                 res.redirect("/login");             });         } else {             res.redirect("/login");         }     }  }; 

//route

app.get('/dashboard_client',isauthenticated('admin'), dashboard_client_view_controller.displayview); 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -