Skip to content

Commit

Permalink
fix: user model (#18)
Browse files Browse the repository at this point in the history
* fix: user Model add is_verified

* fix: error message

* fix: verifyController
  • Loading branch information
tusharbansal22 authored Apr 8, 2024
1 parent 9c35896 commit a50ddfd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
9 changes: 9 additions & 0 deletions controllers/userAuth.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -31,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");
Expand All @@ -47,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",
Expand Down Expand Up @@ -160,6 +165,10 @@ exports.login = async (req, res, next) => {
return;
}

if(!user.is_verified){
next({ status: 401, message: USER_NOT_VERIFIED });
}

const passwordMatch = password === user.password ? 1 : 0;

if (passwordMatch) {
Expand Down
4 changes: 3 additions & 1 deletion errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -23,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"
4 changes: 4 additions & 0 deletions models/user.credentials.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const userCredentialsSchema = new Schema({
type: String,
required: true,
},
is_verified: {
type: Boolean,
default: false,
}
});

module.exports = model("UserCredentials", userCredentialsSchema);

0 comments on commit a50ddfd

Please sign in to comment.