From ef58c4801714255959e671d209beae3853b424aa Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Sat, 1 Aug 2020 14:59:08 +0530 Subject: [PATCH] [fix] error if user not found/invalid pass --- index.js | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 529fa68..2926bc3 100644 --- a/index.js +++ b/index.js @@ -97,14 +97,31 @@ const getProot = () => } const user = await User.findOne({ username: req.body.username }); - const valid = await bcrypt.compare(req.body.password, user.password); + if (user) { + const valid = await bcrypt.compare( + req.body.password, + user.password, + ); - if (valid) { - const token = uuid(); - user.tokens.push(token); - await user.save(); + if (valid) { + const token = uuid(); + user.tokens.push(token); + await user.save(); - res.send({ success: true, token }); + res.send({ success: true, token }); + } else { + res.statusCode = 401; + res.send({ + success: false, + msg: "Error! Username or password was wrong.", + }); + } + } else { + res.statusCode = 401; + res.send({ + success: false, + msg: "Error! Username or password was wrong.", + }); } });