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