2016-09-28 22 views
5

Tôi có một yêu cầu POST với thông tin người dùng như một đối tượng từ một trang đăng nhập và được thông qua để Server API như vậy:MongoDB + Express: Cách xác minh bằng chứng xác thực đăng nhập bằng db.collection(). FindOne() hoặc .find()?

loginUser(creds) { 
    //creds is in the form of { username: bob, password: 123 } 

    var request = { 
     method: 'POST', 
     headers: { 
     'Content-Type': 'application/json' 
     }, 
     body: JSON.stringify(creds), 
    } 

    fetch(`http://localhost:3000/api/login`, request) 
    .then(res => res.json()) 
    .then(user => { 
     console.log(user); 
     console.log('Successful') 
    }) 
    .catch(err => { 
     console.log('Error is', err) 
    }) 
    }, 

Và API Server nhận nó như vậy:

//With .findOne() 
    app.post('/api/login/', function(req, res) { 
     console.log('Req body in login ', req.body) 
     db.collection('users').findOne(req.body, function(err, isMatch) { 
     console.log('ISMATCH IS: ' + isMatch) 
     if(err) { 
      console.log('THIS IS ERROR RESPONSE') 
      res.json(err) 
     } else { 
      console.log('THIS IS ISMATCH RESPONSE') 
      res.json(isMatch) 
     } 
     }) 
    }) 

hoặc

//With .find() 
    app.post('/api/login/', function(req, res) { 
     console.log('Req body in login ', req.body) 
     //console logs correctly as { username: bob, password: 123 } 
     db.collection('users').find(req.body).next(function(err, isMatch) { 
     console.log('ISMATCH IS: ' + isMatch) 
     if(err) { 
      console.log('THIS IS ERROR RESPONSE') 
      res.json(err) 
     } else { 
      console.log('THIS IS ISMATCH RESPONSE') 
      res.json(isMatch) 
     } 
     }) 
    }) 

Vì vậy, với thông tin đăng nhập được chuyển vào, bên trong máy chủ API, tôi muốn tìm kiếm cơ sở dữ liệu 'users' để xem bất kỳ kết quả nào phù hợp với thông tin được truyền vào. Nhưng trong cả hai trường hợp, isMatch luôn là null và luôn ghi nhật ký console.log('THIS IS ISMATCH RESPONSE') ngay cả khi thông tin đăng nhập của người dùng không khớp với bất kỳ thông tin nào trong cơ sở dữ liệu. Và ở phía khách hàng, tôi không bao giờ nhận được bất kỳ phản hồi lỗi nào và luôn ghi lại console.log('Successful').

Dường như không biết tôi đang thiếu gì. Tôi có thể làm gì sai?

Cảm ơn bạn

Trả lời

1

tôi đề nghị bạn để tìm người dùng đầu tiên và sau đó so sánh mật khẩu, một cái gì đó như:

db.collection('users').findOne({ username: req.body.username}, function(err, user) { 
     console.log('User found '); 
     // In case the user not found 
     if(err) { 
      console.log('THIS IS ERROR RESPONSE') 
      res.json(err) 
     } 
     if (user && user.password === req.body.password){ 
      console.log('User and password is correct') 
      res.json(user); 
     } else { 
      console.log("Credentials wrong"); 
      res.json({data: "Login invalid"}); 
     }    
}); 
+0

Nó hoạt động tốt. :) Nhưng nó không có vẻ là cách được đề xuất vì nó so sánh các thông tin đăng nhập với mã, chứ không phải là máy chủ cơ sở dữ liệu chính nó. –

+1

Cảm ơn bạn @NaveenKumarV, tôi nghĩ câu trả lời của bạn sẽ bổ sung cho tôi. +1 –

1

Bạn nên sử dụng $and Operator trong phương pháp .findOne().

dụ làm việc:

db.collection('users').findOne( 

    { $and: [ 
     { name: req.body.username.toLowerCase() }, 
     { password: req.body.password } 
    ] }, 

    (err, result) => { 

    if(err) { 
     res.status(500).send(err); 
     return; 
    } 

    if(!result) { 
     data = { 
      "meta": { 
       "status": "fail", 
       "message": "Login Failure: Invalid username or password" 
      } 
     }; 
     res.status(401).send(data); 
    } else { 
     data = { 
      "meta": { 
       "status": "success", 
       "message": "Login success" 
      } 
     }; 
     res.json(data); 
    } 
}); 
Các vấn đề liên quan