From 694806c5c96fd3c5ce9a63b83c81f78225ec6611 Mon Sep 17 00:00:00 2001 From: Tamal Das Date: Thu, 15 Feb 2024 16:55:51 +0530 Subject: [PATCH] fix: changes to auth routes --- routes/user/Auth.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/routes/user/Auth.js b/routes/user/Auth.js index cd8baa2..c1b3683 100644 --- a/routes/user/Auth.js +++ b/routes/user/Auth.js @@ -1,3 +1,4 @@ +/* eslint-disable no-unused-vars */ const express = require("express"); const router = express.Router(); const jwt = require("jsonwebtoken"); @@ -43,10 +44,13 @@ router.post("/signup", async (req, res) => { email, password: hashedPassword, }); + await newUser.save(); const payload = { User: { id: email } }; const token = jwt.sign(payload, process.env.JWT_SECRET); + const { password, _id, ...userWithoutSensitiveInfo } = newUser.toObject(); + const user = { ...userWithoutSensitiveInfo }; res .status(STATUSCODE.CREATED) @@ -56,6 +60,7 @@ router.post("/signup", async (req, res) => { .cookie("usertype", data?.usertype, frontendCookie) .json({ message: STATUSMESSAGE.SIGNUP_SUCCESS, + user, }); } catch (err) { res.status(STATUSCODE.INTERNAL_SERVER_ERROR).json({ message: err }); @@ -67,6 +72,13 @@ router.post("/signin", async (req, res) => { try { const { email, password } = req.body; const existingUser = await User.findOne({ email }); + let user; + + if (existingUser) { + const { password, _id, __v, ...userWithoutSensitiveInfo } = + existingUser.toObject(); + user = { ...userWithoutSensitiveInfo }; + } if (!existingUser) { return res @@ -92,6 +104,7 @@ router.post("/signin", async (req, res) => { .cookie("usertype", existingUser.usertype, frontendCookie) .json({ message: STATUSMESSAGE.LOGIN_SUCCESS, + user, }); } catch (err) { res.status(STATUSCODE.INTERNAL_SERVER_ERROR).json({ message: err });