From 080768bf5d278c8ffea0c26c20a2288dccd834c3 Mon Sep 17 00:00:00 2001 From: A-nirvana Date: Sun, 7 Apr 2024 15:30:23 +0530 Subject: [PATCH] Added logic to validate Date of Birth --- controllers/authControllers.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/controllers/authControllers.ts b/controllers/authControllers.ts index e9b0eb9..dfc6f74 100644 --- a/controllers/authControllers.ts +++ b/controllers/authControllers.ts @@ -8,13 +8,27 @@ import { Types } from "mongoose"; const signup = async (req: Request, res: Response, next: NextFunction) => { try { let user; - const { email } = req.body; + const { email,dob } = req.body; user = await UserModel.UserSchema.findOne({ email: email }); if (user) { return res.status(httpStatus.BAD_REQUEST).send({ message: "Email already in use", }); } + if(!isNaN(Date.parse(dob))){ + const dobDate = new Date(dob); + const currentDate = new Date(); + if(dobDate >= currentDate){ + return res.status(httpStatus.BAD_REQUEST).send({ + message: "Invalid date of birth", + }); + } + } + else{ + return res.status(httpStatus.BAD_REQUEST).send({ + message: "Invalid date of birth", + }); + } user = await UserModel.UserSchema.create(req.body); const accessToken = jwt.sign(user.toJSON(), process.env.JWT_SECRET); return res.status(httpStatus.CREATED).json({