Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions src/controllers/Auth/index.ts
Original file line number Diff line number Diff line change
@@ -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<string> => {
Expand Down Expand Up @@ -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));
Expand All @@ -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));
Expand Down
3 changes: 1 addition & 2 deletions src/routes/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ 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);
router.put("/resetpassword/:token", checkAuth, resetPassword);
router.get("/user", checkAuth, getUser);

export default router;

4 changes: 2 additions & 2 deletions src/routes/mentorRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
6 changes: 3 additions & 3 deletions src/routes/studentRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;