Skip to content

Commit

Permalink
fix: changes to auth routes
Browse files Browse the repository at this point in the history
  • Loading branch information
tamalCodes committed Feb 15, 2024
1 parent 75a7142 commit 694806c
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions routes/user/Auth.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-unused-vars */
const express = require("express");
const router = express.Router();
const jwt = require("jsonwebtoken");
Expand Down Expand Up @@ -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)
Expand All @@ -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 });
Expand All @@ -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
Expand All @@ -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 });
Expand Down

0 comments on commit 694806c

Please sign in to comment.