I am currently working on developing a simple authentication system. The data in my MongoDB database looks like this:
{
"__v": 0,
"password": "4c1b9bb3405f53cf46731af89f07b01d1ffe974f944d81085cb962abc45ee9c3",
"_id": "589f5ee85a3b61176c9d0a61"
}
Below are the functions I have written to check this data:
router.post('/api/admin/login', (req, res) => {
db.getRootToken(req.body).then(function(data) {
if (data.error) {
return res.status(403).send(data);
} else {
return res.status(200).send(data);
}
});
});
function getRootToken(data) {
console.log(data.password);
var password_sha256 = sha256(data.password);
console.log(password_sha256);
Root.findOne({ password: password_sha256 }, function(err, docs) {
console.log(docs.length);
if (docs.length) {
console.log("root found");
return { token: password_sha256 };
} else {
console.log("root not found");
return { error: "Incorrect password!" };
}
});
}
However, when I try to use /api/admin/login
with { "password" : "pa55word" }
, I encounter this error:
TypeError: Cannot read property "then" of undefined
The application is unable to find any data because the code returns "root not found", even though the hash function generates
4c1b9bb3405f53cf46731af89f07b01d1ffe974f944d81085cb962abc45ee9c3
for "pa55word", which matches the value in the database.
I am looking for guidance on identifying and correcting these errors. Any help would be appreciated.