add the redis
This commit is contained in:
+156
-198
@@ -9,7 +9,7 @@
|
||||
|
||||
// app/controllers/user.controller.js
|
||||
|
||||
const { Op } = require("sequelize");
|
||||
// const { Op } = require("sequelize");
|
||||
const db = require("../models");
|
||||
const { hashPassword } = require("../utils/hashPassword.util");
|
||||
const { validatePassword } = require("../utils/validation/validatePassword.util");
|
||||
@@ -17,10 +17,11 @@ const { validateEmail } = require("../utils/validation/validateEmail.util");
|
||||
const { generateUserId, generateId } = require("../utils/idGen.util");
|
||||
const { logActivity } = require("../services/activity.service");
|
||||
const { sendMail } = require("../utils/mail.util");
|
||||
const {generateEmailVerificationToken, hashEmailVerificationToken} = require("../utils/emailVerification.util");
|
||||
const {
|
||||
createEmailVerification, verifyEmailVerificationToken, deleteEmailVerification} = require("../utils/emailVerification.util");;
|
||||
const User = db.User;
|
||||
const Profile = db.Profile;
|
||||
const EmailVerification = db.EmailVerification;
|
||||
// const EmailVerification = db.EmailVerification;
|
||||
|
||||
// Create a new user
|
||||
exports.createNewUser = async (req, res) => {
|
||||
@@ -84,20 +85,8 @@ if (!emailValid) {
|
||||
}
|
||||
|
||||
const hashedPassword = await hashPassword(password);
|
||||
|
||||
const userID = generateUserId();
|
||||
const verificationToken = generateEmailVerificationToken();
|
||||
|
||||
const verificationTokenHash =
|
||||
hashEmailVerificationToken(
|
||||
verificationToken
|
||||
);
|
||||
|
||||
const verificationExpiresAt =
|
||||
new Date(
|
||||
Date.now() +
|
||||
30 * 60 * 1000
|
||||
);
|
||||
|
||||
|
||||
const newUser = await User.create(
|
||||
{
|
||||
@@ -128,37 +117,13 @@ if (!emailValid) {
|
||||
{ transaction },
|
||||
);
|
||||
|
||||
await EmailVerification.create(
|
||||
{
|
||||
id:
|
||||
generateId(),
|
||||
|
||||
user_id:
|
||||
newUser.id,
|
||||
|
||||
tokenHash:
|
||||
verificationTokenHash,
|
||||
|
||||
expiresAt:
|
||||
verificationExpiresAt,
|
||||
|
||||
usedAt:
|
||||
null,
|
||||
},
|
||||
{
|
||||
transaction,
|
||||
}
|
||||
);
|
||||
|
||||
await transaction.commit();
|
||||
|
||||
const verificationToken = await createEmailVerification(newUser.id);
|
||||
|
||||
const confirmationLink =
|
||||
`${process.env.FRONTEND_URL}/verify-email?token=${verificationToken}`;
|
||||
|
||||
|
||||
// ==================================================
|
||||
// 18. Send verification email
|
||||
// ==================================================
|
||||
|
||||
try {
|
||||
|
||||
@@ -188,15 +153,6 @@ if (!emailValid) {
|
||||
|
||||
} catch (mailError) {
|
||||
|
||||
// The database transaction is already committed.
|
||||
//
|
||||
// Do NOT delete the user here.
|
||||
//
|
||||
// User remains:
|
||||
// PENDING_VERIFICATION
|
||||
//
|
||||
// Later resend-verification can send another email.
|
||||
|
||||
console.error(
|
||||
"VERIFICATION EMAIL ERROR:",
|
||||
mailError
|
||||
@@ -246,13 +202,20 @@ if (!emailValid) {
|
||||
firstName: newUser.firstName,
|
||||
lastName: newUser.lastName,
|
||||
email: newUser.email,
|
||||
accountType: newUser.accountType,
|
||||
// accountType: newUser.accountType,
|
||||
// role: newUser.role,
|
||||
// department: newUser.department,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
await transaction.rollback();
|
||||
if (!transaction.finished) {
|
||||
await transaction.rollback();
|
||||
}
|
||||
|
||||
console.error(
|
||||
"CREATE USER ERROR:",
|
||||
error
|
||||
);
|
||||
|
||||
res.status(500).send({
|
||||
success: false,
|
||||
@@ -263,173 +226,168 @@ if (!emailValid) {
|
||||
};
|
||||
|
||||
// Verify customer email
|
||||
exports.verifyEmail = async (req, res) => {
|
||||
exports.verifyEmail =
|
||||
async (req, res) => {
|
||||
|
||||
const transaction =
|
||||
await db.sequelize.transaction();
|
||||
|
||||
try {
|
||||
|
||||
// 1. Get token from request body
|
||||
const {
|
||||
token
|
||||
} = req.body;
|
||||
const transaction =
|
||||
await db.sequelize.transaction();
|
||||
|
||||
|
||||
// 2. Token is required
|
||||
if (!token) {
|
||||
try {
|
||||
const {
|
||||
token,
|
||||
} = req.body;
|
||||
|
||||
await transaction.rollback();
|
||||
|
||||
return res.status(400).send({
|
||||
success: false,
|
||||
message:
|
||||
"Verification token is required",
|
||||
if (!token) {
|
||||
|
||||
await transaction.rollback();
|
||||
|
||||
|
||||
return res.status(400).send({
|
||||
success:
|
||||
false,
|
||||
|
||||
message:
|
||||
"Verification token is required",
|
||||
});
|
||||
}
|
||||
|
||||
const verification =
|
||||
await verifyEmailVerificationToken(
|
||||
token
|
||||
);
|
||||
|
||||
if (!verification) {
|
||||
|
||||
await transaction.rollback();
|
||||
|
||||
|
||||
return res.status(400).send({
|
||||
success:
|
||||
false,
|
||||
|
||||
message:
|
||||
"Verification token is invalid or expired",
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
const {
|
||||
userId,
|
||||
redisKey,
|
||||
} =
|
||||
verification;
|
||||
|
||||
const user =
|
||||
await User.findOne({
|
||||
where: {
|
||||
id:
|
||||
userId,
|
||||
},
|
||||
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
|
||||
if (!user) {
|
||||
|
||||
await transaction.rollback();
|
||||
|
||||
await deleteEmailVerification(
|
||||
redisKey
|
||||
);
|
||||
|
||||
|
||||
return res.status(404).send({
|
||||
success:
|
||||
false,
|
||||
|
||||
message:
|
||||
"User not found",
|
||||
});
|
||||
}
|
||||
|
||||
if (
|
||||
user.accountStatus ===
|
||||
"ACTIVE" &&
|
||||
user.emailVerifiedAt
|
||||
) {
|
||||
|
||||
await transaction.rollback();
|
||||
|
||||
|
||||
// Token is no longer needed
|
||||
await deleteEmailVerification(
|
||||
redisKey
|
||||
);
|
||||
|
||||
|
||||
return res.status(400).send({
|
||||
success:
|
||||
false,
|
||||
|
||||
message:
|
||||
"Email is already verified",
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
user.accountStatus =
|
||||
"ACTIVE";
|
||||
|
||||
|
||||
user.emailVerifiedAt =
|
||||
new Date();
|
||||
|
||||
|
||||
await user.save({
|
||||
transaction,
|
||||
});
|
||||
}
|
||||
|
||||
await transaction.commit();
|
||||
|
||||
// 3. Hash the received raw token
|
||||
const tokenHash =
|
||||
hashEmailVerificationToken(
|
||||
token
|
||||
await deleteEmailVerification(
|
||||
redisKey
|
||||
);
|
||||
|
||||
|
||||
// 4. Find matching valid token
|
||||
const verification =
|
||||
await EmailVerification.findOne({
|
||||
|
||||
where: {
|
||||
return res.status(200).send({
|
||||
success:
|
||||
true,
|
||||
|
||||
tokenHash:
|
||||
tokenHash,
|
||||
|
||||
usedAt:
|
||||
null,
|
||||
|
||||
expiresAt: {
|
||||
[Op.gt]:
|
||||
new Date(),
|
||||
},
|
||||
},
|
||||
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
// 5. Token invalid / expired / already used
|
||||
if (!verification) {
|
||||
|
||||
await transaction.rollback();
|
||||
|
||||
return res.status(400).send({
|
||||
success: false,
|
||||
message:
|
||||
"Verification token is invalid or expired",
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// 6. Find user
|
||||
const user =
|
||||
await User.findOne({
|
||||
|
||||
where: {
|
||||
id:
|
||||
verification.user_id,
|
||||
},
|
||||
|
||||
transaction,
|
||||
"Email verified successfully. Your account is now active.",
|
||||
});
|
||||
|
||||
|
||||
// 7. User not found
|
||||
if (!user) {
|
||||
} catch (error) {
|
||||
|
||||
await transaction.rollback();
|
||||
if (!transaction.finished) {
|
||||
|
||||
await transaction.rollback();
|
||||
|
||||
}
|
||||
|
||||
|
||||
console.error(
|
||||
"VERIFY EMAIL ERROR:",
|
||||
error
|
||||
);
|
||||
|
||||
|
||||
return res.status(500).send({
|
||||
success:
|
||||
false,
|
||||
|
||||
return res.status(404).send({
|
||||
success: false,
|
||||
message:
|
||||
"User not found",
|
||||
"Failed to verify email",
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// 8. Check already verified
|
||||
if (
|
||||
user.accountStatus === "ACTIVE" &&
|
||||
user.emailVerifiedAt
|
||||
) {
|
||||
|
||||
await transaction.rollback();
|
||||
|
||||
return res.status(400).send({
|
||||
success: false,
|
||||
message:
|
||||
"Email is already verified",
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// 9. Activate account
|
||||
user.accountStatus =
|
||||
"ACTIVE";
|
||||
|
||||
user.emailVerifiedAt =
|
||||
new Date();
|
||||
|
||||
|
||||
await user.save({
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
// 10. Mark token as used
|
||||
verification.usedAt =
|
||||
new Date();
|
||||
|
||||
|
||||
await verification.save({
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
||||
// 11. Commit
|
||||
await transaction.commit();
|
||||
|
||||
|
||||
// 12. Success
|
||||
return res.status(200).send({
|
||||
success: true,
|
||||
message:
|
||||
"Email verified successfully. Your account is now active.",
|
||||
});
|
||||
|
||||
|
||||
} catch (error) {
|
||||
|
||||
if (!transaction.finished) {
|
||||
|
||||
await transaction.rollback();
|
||||
|
||||
}
|
||||
|
||||
|
||||
console.error(
|
||||
"VERIFY EMAIL ERROR:",
|
||||
error
|
||||
);
|
||||
|
||||
|
||||
return res.status(500).send({
|
||||
success: false,
|
||||
message:
|
||||
"Failed to verify email",
|
||||
});
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
// Get users with pagination (20 per page)
|
||||
exports.getAllUsers = async (req, res) => {
|
||||
|
||||
Reference in New Issue
Block a user