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

python - Specify path of savefig with pylab or matplotlib -

c# - SharpSsh Command Execution -

How to run C# code using mono without Xamarin in Android? -