Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: user model #18

Merged
merged 3 commits into from
Apr 8, 2024
Merged
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
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);