javascript - Node js custom callback function error -
i'm trying make simple authentication node js. because read user data database, have make asynchronous. here's function, checks if authentication ok:
function auth(req, callback) { var header = req.headers['authorization']; console.log(cb.type); console.log("authorization header is: ", header); if(!header) { callback(false); } else if(header) { var tmp = header.split(' '); var buf = new buffer(tmp[1], 'base64'); var plain_auth = buf.tostring(); console.log("decoded authorization ", plain_auth); var creds = plain_auth.split(':'); var name = creds[0]; var password = creds[1]; user.findone({name:name, password:password}, function(err, user) { if (user){ callback(true); }else { callback(false); } }); } }
and here call function:
auth (req, function (success){ if (!success){ res.setheader('www-authenticate', 'basic realm="myrealm'); res.status(401).send("unauthorized"); }else{ if(user!==req.user) { res.status(403).send("unauthorized"); }else{ user.findoneandupdate({user:userid}, {user:req.body.user, name:req.body.name, email:req.user.email, password:user.generatehash(req.body.password)}, {upsert:true}, function(err, user) { if(!err) { res.status(200).send("ok"); }else{ res.status(400).send("error"); } }); } } });
this gives me error "typeerror: object not function", pointing @ "callback(false)". have no idea cause error, pass function parameter, , first log message prints "[function]". appreciated.
Comments
Post a Comment