Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. router.post('/api/users/authenticate', function(req, res, next)
  2. {
  3.  
  4.  
  5.     var username = req.body.username;
  6.     var password = req.body.password;
  7.  
  8.     console.log("post authentication request received.\n");
  9.  
  10.     console.log("username is: " + username + "\n");
  11.     console.log("password is: " + password + "\n");
  12.  
  13.  
  14.     var url = 'mongodb://localhost/admin';
  15.  
  16.     var authenticate = function(db, callback)
  17.     {
  18.  
  19.  
  20.  
  21.         var cursor = db.collection('users').find
  22.         (
  23.  
  24.  
  25.  
  26.             {"username" : username, "password" : password}
  27.                         //{"username" : "user", "password" : "pass"}
  28.                         //{"_id" : ObjectId("580c074cffc27c14fffe126c")}
  29.  
  30.  
  31.  
  32.  
  33.  
  34.         );
  35.  
  36.         var array = new Array();
  37.  
  38.  
  39.         var isAuthenticated = false;
  40.  
  41.         cursor.each(function(err,doc)
  42.         {
  43.  
  44.                 if (err)
  45.                 {
  46.                     console.log(err);
  47.                    
  48.                 }
  49.                 else
  50.                 {
  51.                     console.log('Fetched:', doc);
  52.  
  53.                     isAuthenticated = true;
  54.  
  55.                     console.log("succesfully authenticated1. ");
  56.                    
  57.                     console.log("isAuthenticated1 is: " + isAuthenticated + "\n");
  58.                    
  59.                    
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.                 }
  67.  
  68.  
  69.  
  70.  
  71.         });
  72.        
  73.  
  74.         callback(isAuthenticated);
  75.                
  76.  
  77.  
  78.  
  79.     };
  80.  
  81.     MongoClient.connect(url, function(err, db)
  82.     {
  83.         assert.equal(null, err);
  84.         authenticate(db, function(isAuthenticated)
  85.         {
  86.  
  87.             //res.writeHead(200, {"Content-Type": "text/plain"});
  88.  
  89.             console.log("callback ran <<<<<<<<<-----------");
  90.             console.log("isAuthenticated3 is: " + isAuthenticated + "\n");
  91.  
  92.             if (isAuthenticated == true)
  93.             {
  94.                     res.json({authenticated: 'true'});         
  95.             }
  96.  
  97.  
  98.             else
  99.             {
  100.  
  101.                     res.json({authenticated: 'false'});
  102.  
  103.             }
  104.  
  105.  
  106.             //res.end(JSON.stringify(array));
  107.             //res.json(JSON.stringify(array));
  108.  
  109.  
  110.             db.close();
  111.         });
  112.     });
  113.  
  114.  
  115.  
  116.     //res.json({status: 'user added'});
  117.  
  118.     //console.log("price: " + price);
  119.  
  120.     //res.json({'message': 'product put', 'price': price});
  121.  
  122.  
  123.  
  124. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement