Question: Javascript Help: Get rid of the hard-coded username and password values (admin/waketech). Instead read the credential from a text file called auth.json, which stores the
Javascript Help:
Get rid of the hard-coded username and password values (admin/waketech). Instead read the credential from a text file called auth.json, which stores the credential as a json object.
// programmer defined function to return 401 basic authentication required function send401() { res.writeHead(401, {'WWW-Authenticate': 'Basic'}); res.end(); } // get authorization info from request headers var authHeader = req.headers.authorization; if (!authHeader) { send401(); return; } // extract user and password info that is base64 encoded var auth = new Buffer(authHeader.split(' ')[1], 'base64').toString().split(':'); var user = auth[0]; var pass = auth[1]; // verify the credential with hard-coded values if (user == 'admin' && pass == 'waketech') { // Retrieve all feedback documents from MongoDB MongoClient.connect('mongodb://127.0.0.1:27017/local', function (err, db){ if (err) throw err; console.log('connected to mongodb://127.0.0.1:27017/local'); db.collection('feedback').find().toArray(function(err, docs) { createHTMLReturn(docs, res); });
db.close(); }); } else { // credential invalid send401(); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
