From 7c76c9e69f13fcacb5b7e9d8e9ac78ef1ce31070 Mon Sep 17 00:00:00 2001 From: Tushar Date: Mon, 8 Apr 2024 17:44:37 +0530 Subject: [PATCH 1/3] fix: user Model add is_verified --- controllers/userAuth.controller.js | 4 ++++ models/user.credentials.js | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/controllers/userAuth.controller.js b/controllers/userAuth.controller.js index 37eebcb..497ea16 100644 --- a/controllers/userAuth.controller.js +++ b/controllers/userAuth.controller.js @@ -160,6 +160,10 @@ exports.login = async (req, res, next) => { return; } + if(!user.is_verified){ + next({ status: 401, message: ACCESS_DENIED_ERR }); + } + const passwordMatch = password === user.password ? 1 : 0; if (passwordMatch) { diff --git a/models/user.credentials.js b/models/user.credentials.js index 9636066..43acfc5 100644 --- a/models/user.credentials.js +++ b/models/user.credentials.js @@ -17,6 +17,10 @@ const userCredentialsSchema = new Schema({ type: String, required: true, }, + is_verified: { + type: Boolean, + default: false, + } }); module.exports = model("UserCredentials", userCredentialsSchema); From b6e20ec70c42a9ce8c6b128501656b092cb44719 Mon Sep 17 00:00:00 2001 From: Tushar Date: Mon, 8 Apr 2024 17:49:57 +0530 Subject: [PATCH 2/3] fix: error message --- controllers/userAuth.controller.js | 3 ++- errors.js | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/controllers/userAuth.controller.js b/controllers/userAuth.controller.js index 497ea16..1c51096 100644 --- a/controllers/userAuth.controller.js +++ b/controllers/userAuth.controller.js @@ -21,6 +21,7 @@ const { ACCESS_DENIED_ERR, EMAIL_NOT_FOUND_ERR, OTP_EXPIRED_ERR, + USER_NOT_VERIFIED, } = require("../errors"); const { createJwtToken } = require("../utils/token.util"); @@ -161,7 +162,7 @@ exports.login = async (req, res, next) => { } if(!user.is_verified){ - next({ status: 401, message: ACCESS_DENIED_ERR }); + next({ status: 401, message: USER_NOT_VERIFIED }); } const passwordMatch = password === user.password ? 1 : 0; diff --git a/errors.js b/errors.js index bfef295..091a7b4 100644 --- a/errors.js +++ b/errors.js @@ -10,6 +10,8 @@ exports.JWT_DECODE_ERR = "incorrect token"; exports.USER_NOT_FOUND_ERR = "User not found"; +exports.USER_NOT_VERIFIED = 'Please complete email verification' + exports.ACCESS_DENIED_ERR = "Access deny for normal user"; exports.Email_NOT_FOUND_ERR = "email not found"; From 846edff8e379fe26c4eca2f9886bb5c4ae3a6b0b Mon Sep 17 00:00:00 2001 From: Tushar Date: Mon, 8 Apr 2024 18:02:31 +0530 Subject: [PATCH 3/3] fix: verifyController --- controllers/userAuth.controller.js | 4 ++++ errors.js | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/controllers/userAuth.controller.js b/controllers/userAuth.controller.js index 1c51096..160400b 100644 --- a/controllers/userAuth.controller.js +++ b/controllers/userAuth.controller.js @@ -32,6 +32,7 @@ exports.verifyOtp = async (req, res, next) => { const currentDateTime = new Date(); const user = await User.findOne({ email }); + const userCredentials = await UserCredentials.findOne({ email }); if (!user) { next({ status: 400, message: USER_NOT_FOUND_ERR }); console.log("user not found"); @@ -48,10 +49,13 @@ exports.verifyOtp = async (req, res, next) => { } if (otp.expiresAt < currentDateTime) { next({ status: 400, message: OTP_EXPIRED_ERR }); + await user.deleteOne(); + await userCredentials.deleteOne(); return; } const token = createJwtToken({ userId: user._id }); + await userCredentials.updateOne({ is_verified: true }); res.status(201).json({ type: "success", diff --git a/errors.js b/errors.js index 091a7b4..88ce8ff 100644 --- a/errors.js +++ b/errors.js @@ -25,6 +25,6 @@ exports.INCORRECT_CRED_ERR = exports.EMAIL_NOT_FOUND_ERR = "Email not found"; exports.ADMIN_NOT_FOUND = "Admin not found"; -exports.OTP_EXPIRED_ERR = "OTP has expired"; +exports.OTP_EXPIRED_ERR = "OTP has expired. Re-Register to continue"; exports.VENDOR_NOT_PERMITTED = "Vendor is not verified or has been debarred" \ No newline at end of file