From 1b020a3f50b8317b46d336569fb8b8290855dcff Mon Sep 17 00:00:00 2001 From: Mateja Date: Sat, 24 Jul 2021 23:18:08 +0200 Subject: Return token on registration, return admin status on login, 404 on user not found. --- server/controllers/user.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'server/controllers') 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 }); } }); -- cgit v1.2.3