From a10e532710a372c543c3daf8fa8696fe45caf4e4 Mon Sep 17 00:00:00 2001 From: ShivaniYadav07 Date: Thu, 17 Oct 2024 22:40:36 +0530 Subject: [PATCH 1/2] setting up routes for dashboard --- src/controllers/Auth/index.ts | 40 +++++++++++++++++++++++++++-------- src/routes/auth.ts | 3 +-- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/controllers/Auth/index.ts b/src/controllers/Auth/index.ts index 9782499..4ab625d 100644 --- a/src/controllers/Auth/index.ts +++ b/src/controllers/Auth/index.ts @@ -1,9 +1,9 @@ +import jwt from 'jsonwebtoken'; import { Request, Response, NextFunction } from "express"; import User from "../../models/userModel"; import { CustomError } from "../../middleware/error"; import setCookie from "../../utils/setCookies"; import crypto from "crypto"; -// import { db } from "../../db/db"; import { sendMail } from "../../utils/sendMail"; const hashPassword = (password: string, salt: string): Promise => { @@ -38,7 +38,18 @@ export const register = async (req: Request, res: Response, next: NextFunction) await newUser.save(); - res.status(201).json({ message: 'User registered successfully' }); + // Generate JWT token + const token = jwt.sign( + { userId: newUser._id, email: newUser.email }, + process.env.JWT_SECRET!, + { expiresIn: '1h' } // Token valid for 1 hour + ); + + // Send response with token + res.status(201).json({ + message: 'User registered successfully', + token, // Return the token to the client + }); } catch (error: any) { console.log(error); next(new CustomError(error.message)); @@ -60,14 +71,25 @@ export const login = async (req: Request, res: Response, next: NextFunction) => return res.status(400).json({ message: 'Invalid email or password' }); } - setCookie({ - user, - res, - next, - message: "Login Success", - statusCode: 200, + // Generate JWT token + const token = jwt.sign( + { userId: user._id, email: user.email }, + process.env.JWT_SECRET!, // Ensure your JWT secret is available in environment variables + { expiresIn: '1h' } // Token valid for 1 hour + ); + + // Optionally set the token as an HTTP-only cookie + res.cookie('token', token, { + httpOnly: true, + secure: process.env.NODE_ENV === 'production', // Only send over HTTPS in production + maxAge: 3600000, // 1 hour + }); + + // Send response with token + res.status(200).json({ + message: 'Logged in successfully', + token, // Return the token to the client }); - res.status(200).json({ message: 'Logged in successfully' }); } catch (error: any) { console.log(error); next(new CustomError(error.message)); diff --git a/src/routes/auth.ts b/src/routes/auth.ts index 986e0c2..4557705 100644 --- a/src/routes/auth.ts +++ b/src/routes/auth.ts @@ -12,8 +12,6 @@ import { checkAuth } from "../middleware/checkAuth"; const router = express.Router(); router.post("/register", register); -// router.post("/verify", otpVerification); -// router.post("/resend", resentOtp); router.post("/login", login); router.get("/logout", checkAuth, logout); router.post("/forgetpassword", forgotPassword); @@ -21,3 +19,4 @@ router.put("/resetpassword/:token", checkAuth, resetPassword); router.get("/user", checkAuth, getUser); export default router; + \ No newline at end of file From 1ff23acfec1654cfbe0d5e2aebeaa063fcd3c1ea Mon Sep 17 00:00:00 2001 From: ShivaniYadav07 Date: Sat, 19 Oct 2024 23:50:16 +0530 Subject: [PATCH 2/2] update the routes for mentor and student --- src/routes/mentorRoutes.ts | 4 ++-- src/routes/studentRoute.ts | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/routes/mentorRoutes.ts b/src/routes/mentorRoutes.ts index 459e81c..7ba8bc1 100644 --- a/src/routes/mentorRoutes.ts +++ b/src/routes/mentorRoutes.ts @@ -5,8 +5,8 @@ import { getMentor, getMentorWithStudents, verifyMentor } from "../controllers/M const router = express.Router(); -router.get("/getmentor", checkAuth, getMentor); +router.get("/getmentor", getMentor); router.put("/verify/:id", checkAuth, verifyMentor) -router.get("/getstudent/:id", checkAuth, getMentorWithStudents) +router.get("/getstudent/:id", getMentorWithStudents) export default router; diff --git a/src/routes/studentRoute.ts b/src/routes/studentRoute.ts index c9ad5ed..afeab01 100644 --- a/src/routes/studentRoute.ts +++ b/src/routes/studentRoute.ts @@ -5,8 +5,8 @@ import { getStudentsWithNullMentor, allocateStudents, deallocateStudents} from " const router = express.Router(); -router.post("/allocate-student/:mentorId", checkAuth, allocateStudents) -router.post("/deallocate-student", checkAuth, deallocateStudents) -router.get("/getmentorstudent", checkAuth, getStudentsWithNullMentor) +router.post("/allocate-student/:mentorId", allocateStudents) +router.post("/deallocate-student", deallocateStudents) +router.get("/getmentorstudent", getStudentsWithNullMentor) export default router;