Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. router.post('/api/users/authenticate', function(req, res, next) {
  2. var username = req.body.username;
  3. var password = req.body.password;
  4.  
  5. console.log("post authentication request received.\n");
  6.  
  7. console.log("username is: " + username + "\n");
  8. console.log("password is: " + password + "\n");
  9.  
  10. var url = 'mongodb://localhost/admin';
  11.  
  12. var authenticate = function(db, callback) {
  13. var cursor = db.collection('users').find({
  14. "username" : username,
  15. "password" : password}
  16. );
  17.  
  18. var array = new Array();
  19.  
  20.  
  21. var isAuthenticated = false;
  22.  
  23. cursor.each(function(err,doc)
  24. {
  25.  
  26. if (err)
  27. {
  28. console.log(err);
  29.  
  30. }
  31. else
  32. {
  33. console.log('Fetched:', doc);
  34.  
  35. isAuthenticated = true;
  36.  
  37. console.log("succesfully authenticated1. ");
  38.  
  39. console.log("isAuthenticated1 is: " + isAuthenticated + "\n");
  40. }
  41. });
  42. callback(isAuthenticated);
  43. };
  44.  
  45. MongoClient.connect(url, function(err, db) {
  46. assert.equal(null, err);
  47. authenticate(db, function(isAuthenticated) {
  48. //res.writeHead(200, {"Content-Type": "text/plain"});
  49.  
  50. console.log("callback ran <<<<<<<<<-----------");
  51. console.log("isAuthenticated3 is: " + isAuthenticated + "\n");
  52.  
  53. if (isAuthenticated == true) {
  54. res.json({authenticated: 'true'});
  55. } else {
  56. res.json({authenticated: 'false'});
  57. }
  58. db.close();
  59. });
  60. });
  61. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement