I recently started working with MongoDB and express. I am trying to authenticate a user based on their username and password, but my code seems to always execute the "else statement" even when the correct credentials are entered.
Below is the JavaScript file snippet:
app.post('/auth', function(req, res){
var user = ( db.collection('auth').findOne({name: req.body.username}));
var pass = ( db.collection('auth').findOne({password: req.body.password}));
if(user == req.body.username && pass == req.body.password){
res.send("Credentials Match");
}else{
res.send("Wrong Credentials");
}
console.log(req.body);
})
And here is the HTML code snippet:
<form class="form-signin" action="/auth" method="POST">
<h1 class="h3 mb-3 font-weight-normal">Please sign in</h1>
<label for="inputEmail" class="sr-only">Username</label>
<input type="text" placeholder="Username" name="username" required="">
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" name="password" placeholder="password" required="">
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>