From 142ca7f482994d2f7d75f4e8aa7e4ad652c92b4d Mon Sep 17 00:00:00 2001 From: ujjwalexcodie Date: Thu, 16 May 2024 02:34:23 +0530 Subject: [PATCH 1/3] Added Zod Validation to the Backend with Some basic Corrections in Autherization File Signed-off-by: ujjwalexcodie --- services/Auth/package.json | 3 ++- .../Auth/src/controllers/auth.controller.js | 2 +- .../Auth/src/middleware/validate.schema.js | 15 +++++++++++ services/Auth/src/routes/auth.route.js | 7 +++--- services/Auth/src/validation/zodschema.js | 25 +++++++++++++++++++ 5 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 services/Auth/src/middleware/validate.schema.js create mode 100644 services/Auth/src/validation/zodschema.js diff --git a/services/Auth/package.json b/services/Auth/package.json index 18c2343..01c5c61 100644 --- a/services/Auth/package.json +++ b/services/Auth/package.json @@ -10,7 +10,8 @@ "express": "^4.19.2", "firebase-admin": "^12.1.0", "jsonwebtoken": "^9.0.2", - "mongoose": "^8.3.3" + "mongoose": "^8.3.3", + "zod": "^3.23.8" }, "scripts": { "dev": "nodemon index.js" diff --git a/services/Auth/src/controllers/auth.controller.js b/services/Auth/src/controllers/auth.controller.js index 52ae93f..6e71bf7 100644 --- a/services/Auth/src/controllers/auth.controller.js +++ b/services/Auth/src/controllers/auth.controller.js @@ -8,7 +8,7 @@ async function signup(req, res) { const { name, email, password } = req.body; //Check if input is as expected or not - if (!email || !password || !email) { + if (!name || !password || !email) { res.json (errorHadnler(401,'All fields must be filled')) return } diff --git a/services/Auth/src/middleware/validate.schema.js b/services/Auth/src/middleware/validate.schema.js new file mode 100644 index 0000000..5d98c70 --- /dev/null +++ b/services/Auth/src/middleware/validate.schema.js @@ -0,0 +1,15 @@ +const validate = (schema) => async (req, res, next) => { + try { + const parsedBody = await schema.parseAsync(req.body); + req.body = parsedBody; + next(); + } catch (err) { + console.log(err); + const yourerror = err.errors[0].message; + res.status(400).json({ + msg: yourerror, + }); + } +} + +module.exports = validate; \ No newline at end of file diff --git a/services/Auth/src/routes/auth.route.js b/services/Auth/src/routes/auth.route.js index f05e2e8..cc64761 100644 --- a/services/Auth/src/routes/auth.route.js +++ b/services/Auth/src/routes/auth.route.js @@ -2,11 +2,12 @@ const express = require("express"); const router = express.Router(); const { signup, signin } = require("../controllers/auth.controller"); - +const validate = require("../middleware/validate.schema"); +const RegisterSchema = require("../validation/zodschema"); // auth routes...; -router.post("/sign-up", signup); +router.post("/sign-up",validate(RegisterSchema), signup); -router.post("/sign-in", signin); +router.post("/sign-in",validate(RegisterSchema), signin); module.exports = router; diff --git a/services/Auth/src/validation/zodschema.js b/services/Auth/src/validation/zodschema.js new file mode 100644 index 0000000..ce6c702 --- /dev/null +++ b/services/Auth/src/validation/zodschema.js @@ -0,0 +1,25 @@ +const zod = require("zod"); + + +const RegisterSchema = zod.object({ + name: zod + .string({ required_error: "Email is required" }) + .trim() + .email({ message: "Invalid Email Address" }) + .min(3, { message: "Email should be at least 3 characters" }) + .max(255, { message: "Email can be at most 255 characters" }), + password: zod + .string({ required_error: "Password is required" }) + .trim() + .min(7, { message: "Password must be at least 7 characters" }) + .max(1024, { message: "Password must be at most 1024 characters" }), + email: zod + .string({ required_error: "Email is required" }) + .trim() + .email({message : "invalide Email Address"}) + .min(3, { message: "Email must be at least of 3 characters" }) + .max(255, { message: "Email must be more than 255 characters" }), + }); + + + module.exports = RegisterSchema ; \ No newline at end of file From 1e35bd60cbcc7435fe24d2d93ea13f964cf19323 Mon Sep 17 00:00:00 2001 From: ujjwalexcodie Date: Fri, 17 May 2024 17:40:38 +0530 Subject: [PATCH 2/3] Corrected ZOD Schema --- services/Auth/src/validation/zodschema.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/services/Auth/src/validation/zodschema.js b/services/Auth/src/validation/zodschema.js index ce6c702..e75f6a9 100644 --- a/services/Auth/src/validation/zodschema.js +++ b/services/Auth/src/validation/zodschema.js @@ -3,6 +3,11 @@ const zod = require("zod"); const RegisterSchema = zod.object({ name: zod + .string({ required_error: "username Is Required" }) + .trim() + .min(3, { message: "Username must be at least 3 characters" }) + .max(255, { message: "Username can be at most 255 characters" }), + email: zod .string({ required_error: "Email is required" }) .trim() .email({ message: "Invalid Email Address" }) @@ -12,13 +17,7 @@ const RegisterSchema = zod.object({ .string({ required_error: "Password is required" }) .trim() .min(7, { message: "Password must be at least 7 characters" }) - .max(1024, { message: "Password must be at most 1024 characters" }), - email: zod - .string({ required_error: "Email is required" }) - .trim() - .email({message : "invalide Email Address"}) - .min(3, { message: "Email must be at least of 3 characters" }) - .max(255, { message: "Email must be more than 255 characters" }), + .max(1024, { message: "Password must be at most 1024 characters" }) }); From 293376d2cfe576178c393b9da125304b5668c05e Mon Sep 17 00:00:00 2001 From: ujjwalexcodie Date: Sat, 18 May 2024 12:14:59 +0530 Subject: [PATCH 3/3] added another Zod Schema for the Sign-in Errors --- services/Auth/src/routes/auth.route.js | 5 +- services/Auth/src/validation/zodschema.js | 57 ++++++++++++++--------- 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/services/Auth/src/routes/auth.route.js b/services/Auth/src/routes/auth.route.js index cc64761..6aa78bd 100644 --- a/services/Auth/src/routes/auth.route.js +++ b/services/Auth/src/routes/auth.route.js @@ -3,11 +3,10 @@ const express = require("express"); const router = express.Router(); const { signup, signin } = require("../controllers/auth.controller"); const validate = require("../middleware/validate.schema"); -const RegisterSchema = require("../validation/zodschema"); +const { RegisterSchema, SigninSchema } = require("../validation/zodschema"); // auth routes...; router.post("/sign-up",validate(RegisterSchema), signup); - -router.post("/sign-in",validate(RegisterSchema), signin); +router.post("/sign-in",validate(SigninSchema), signin); module.exports = router; diff --git a/services/Auth/src/validation/zodschema.js b/services/Auth/src/validation/zodschema.js index e75f6a9..472fad3 100644 --- a/services/Auth/src/validation/zodschema.js +++ b/services/Auth/src/validation/zodschema.js @@ -1,24 +1,39 @@ -const zod = require("zod"); - +const zod = require("zod"); const RegisterSchema = zod.object({ - name: zod - .string({ required_error: "username Is Required" }) - .trim() - .min(3, { message: "Username must be at least 3 characters" }) - .max(255, { message: "Username can be at most 255 characters" }), - email: zod - .string({ required_error: "Email is required" }) - .trim() - .email({ message: "Invalid Email Address" }) - .min(3, { message: "Email should be at least 3 characters" }) - .max(255, { message: "Email can be at most 255 characters" }), - password: zod - .string({ required_error: "Password is required" }) - .trim() - .min(7, { message: "Password must be at least 7 characters" }) - .max(1024, { message: "Password must be at most 1024 characters" }) - }); + name: zod + .string({ required_error: "Username is required" }) + .trim() + .min(3, { message: "Username must be at least 3 characters" }) + .max(255, { message: "Username can be at most 255 characters" }), + email: zod + .string({ required_error: "Email is required" }) + .trim() + .email({ message: "Invalid email address" }) + .min(3, { message: "Email should be at least 3 characters" }) + .max(255, { message: "Email can be at most 255 characters" }), + password: zod + .string({ required_error: "Password is required" }) + .trim() + .min(7, { message: "Password must be at least 7 characters" }) + .max(1024, { message: "Password must be at most 1024 characters" }) +}); + +const SigninSchema = zod.object({ + email: zod + .string({ required_error: "Email is required" }) + .trim() + .email({ message: "Invalid email address" }) + .min(3, { message: "Email should be at least 3 characters" }) + .max(255, { message: "Email can be at most 255 characters" }), + password: zod + .string({ required_error: "Password is required" }) + .trim() + .min(7, { message: "Password must be at least 7 characters" }) + .max(1024, { message: "Password must be at most 1024 characters" }) +}); - - module.exports = RegisterSchema ; \ No newline at end of file +module.exports = { + RegisterSchema, + SigninSchema +};