diff options
author | Mateja <mail@matejamaric.com> | 2021-07-24 23:18:08 +0200 |
---|---|---|
committer | Mateja <mail@matejamaric.com> | 2021-07-24 23:18:08 +0200 |
commit | 1b020a3f50b8317b46d336569fb8b8290855dcff (patch) | |
tree | 353d445ce2640ba3b03bfd47acb76df21ae115b3 | |
parent | 33b833ea689be5c76fad7b288b04e00c4f24462e (diff) | |
download | mevn-ecommerce-1b020a3f50b8317b46d336569fb8b8290855dcff.tar.gz mevn-ecommerce-1b020a3f50b8317b46d336569fb8b8290855dcff.zip |
Return token on registration, return admin status on login, 404 on user
not found.
-rw-r--r-- | server/controllers/user.js | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/server/controllers/user.js b/server/controllers/user.js index e3c1078..141d194 100644 --- a/server/controllers/user.js +++ b/server/controllers/user.js @@ -18,7 +18,10 @@ module.exports = { }); newUser.save() - .then(() => res.json({status: "User successfully registered!"})) + .then(user => res.json({ + status: "User successfully registered!", + token: jwt.sign({sub: user._id}, masterKey, {expiresIn: "1d"}) + })) .catch(err => res.json({ status: "Error when registering user!", error: err @@ -31,6 +34,9 @@ module.exports = { if (err) res.json({status: "Database error.", error: err}); + if (!user) + return res.status(404).json({status: "No such user found!"}); + if (!bcrypt.compareSync(req.body.password, user.password)) res.json({status: "Wrong credentials!"}); else { @@ -38,7 +44,8 @@ module.exports = { const token = jwt.sign(payload, masterKey, {expiresIn: "1d"}); res.json({ status: "Successfully logged in!", - token + token, + isAdmin: user.admin }); } }); |