diff --git a/routes/auth.js b/routes/auth.js index d31a6101ed..8cf078ce61 100644 --- a/routes/auth.js +++ b/routes/auth.js @@ -5,64 +5,58 @@ const jwt = require("jsonwebtoken"); //REGISTER router.post("/register", async (req, res) => { - const newUser = new User({ - username: req.body.username, - email: req.body.email, - password: CryptoJS.AES.encrypt( - req.body.password, - process.env.PASS_SEC - ).toString(), - }); - - try { - const savedUser = await newUser.save(); - res.status(201).json(savedUser); - } catch (err) { - res.status(500).json(err); - } + const newUser = new User({ + username: req.body.username, + email: req.body.email, + password: CryptoJS.AES.encrypt( + req.body.password, + process.env.PASS_SEC + ).toString(), + }); + + try { + const savedUser = await newUser.save(); + res.status(201).json(savedUser); + } catch (err) { + res.status(500).json(err); + } }); //LOGIN -router.post('/login', async (req, res) => { - try{ - const user = await User.findOne( - { - userName: req.body.user_name - } - ); - - !user && res.status(401).json("Wrong User Name"); +router.post("/login", async (req, res) => { + try { + const user = await User.findOne({ + username: req.body.username, + }); - const hashedPassword = CryptoJS.AES.decrypt( - user.password, - process.env.PASS_SEC - ); + !user && res.status(401).json("Wrong User Name"); + const hashedPassword = CryptoJS.AES.decrypt( + user.password, + process.env.PASS_SEC + ); - const originalPassword = hashedPassword.toString(CryptoJS.enc.Utf8); + const originalPassword = hashedPassword.toString(CryptoJS.enc.Utf8); - const inputPassword = req.body.password; - - originalPassword != inputPassword && - res.status(401).json("Wrong Password"); + const inputPassword = req.body.password; - const accessToken = jwt.sign( - { - id: user._id, - isAdmin: user.isAdmin, - }, - process.env.JWT_SEC, - {expiresIn:"3d"} - ); - - const { password, ...others } = user._doc; - res.status(200).json({...others, accessToken}); + originalPassword != inputPassword && res.status(401).json("Wrong Password"); - }catch(err){ - res.status(500).json(err); - } + const accessToken = jwt.sign( + { + id: user._id, + isAdmin: user.isAdmin, + }, + process.env.JWT_SEC, + { expiresIn: "3d" } + ); + const { password, ...others } = user._doc; + res.status(200).json({ ...others, accessToken }); + } catch (err) { + res.status(500).json(err); + } }); module.exports = router;