model bug resolved
This commit is contained in:
parent
2d712682cf
commit
5c8e785d36
@ -1051,156 +1051,156 @@ exports.forgotPasswordVerifyOTP = async (req, res) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
exports.requestOTPForLogin = async (req, res) => {
|
exports.requestOTPForLogin = async (req, res) => {
|
||||||
// try {
|
try {
|
||||||
// const { registered_email } = req.body;
|
const { registered_email } = req.body;
|
||||||
|
|
||||||
// if (!registered_email) {
|
if (!registered_email) {
|
||||||
// return res.status(400).json({
|
return res.status(400).json({
|
||||||
// status: "failed",
|
status: "failed",
|
||||||
// message: "Email is required",
|
message: "Email is required",
|
||||||
// });
|
});
|
||||||
// }
|
}
|
||||||
|
|
||||||
// // Check which model contains the user
|
// Check which model contains the user
|
||||||
// let loggingUser = await EstablishmentUser.findOne({
|
let loggingUser = await EstablishmentUser.findOne({
|
||||||
// where: { email: registered_email }
|
where: { email: registered_email }
|
||||||
// });
|
});
|
||||||
// let userModel = EstablishmentUser;
|
let userModel = EstablishmentUser;
|
||||||
|
|
||||||
// if (!loggingUser) {
|
if (!loggingUser) {
|
||||||
// loggingUser = await User.findOne({
|
loggingUser = await User.findOne({
|
||||||
// where: { email: registered_email, is_active: true }
|
where: { email: registered_email, is_active: true }
|
||||||
// });
|
});
|
||||||
// userModel = User;
|
userModel = User;
|
||||||
// }
|
}
|
||||||
|
|
||||||
// if (!loggingUser) {
|
if (!loggingUser) {
|
||||||
// logger.warn(`OTP requested for non-existing email: ${sanitizeForLog(registered_email)}`);
|
logger.warn(`OTP requested for non-existing email: ${sanitizeForLog(registered_email)}`);
|
||||||
// return res.status(404).json({
|
return res.status(404).json({
|
||||||
// status: "failed",
|
status: "failed",
|
||||||
// message: "Invalid login request. Please check your email or register to continue.",
|
message: "Invalid login request. Please check your email or register to continue.",
|
||||||
// });
|
});
|
||||||
// }
|
}
|
||||||
|
|
||||||
// // Secure OTP generation
|
// Secure OTP generation
|
||||||
// const otp = crypto.randomInt(100000, 999999).toString();
|
const otp = crypto.randomInt(100000, 999999).toString();
|
||||||
|
|
||||||
// // Hash OTP before storing
|
// Hash OTP before storing
|
||||||
// const hashedOtp = await bcrypt.hash(otp, 10);
|
const hashedOtp = await bcrypt.hash(otp, 10);
|
||||||
|
|
||||||
// // Store OTP in the correct model with WHERE clause
|
// Store OTP in the correct model with WHERE clause
|
||||||
// await userModel.update(
|
await userModel.update(
|
||||||
// {
|
{
|
||||||
// login_otp: hashedOtp,
|
login_otp: hashedOtp,
|
||||||
// login_otp_expires_at: new Date(Date.now() + 10 * 60 * 1000),
|
login_otp_expires_at: new Date(Date.now() + 10 * 60 * 1000),
|
||||||
// },
|
},
|
||||||
// {
|
{
|
||||||
// where: { email: registered_email }
|
where: { email: registered_email }
|
||||||
// }
|
}
|
||||||
// );
|
);
|
||||||
// const placeHolderData = {
|
const placeHolderData = {
|
||||||
// username: loggingUser.name,
|
username: loggingUser.name,
|
||||||
// verfication_code: otp,
|
verfication_code: otp,
|
||||||
// support_email: process.env.SUPPORT_EMAIL,
|
support_email: process.env.SUPPORT_EMAIL,
|
||||||
// support_phone: process.env.SUPPORT_PHONE
|
support_phone: process.env.SUPPORT_PHONE
|
||||||
// };
|
};
|
||||||
|
|
||||||
// await sendEmailService(registered_email, "sign_in_verification_code", placeHolderData);
|
await sendEmailService(registered_email, "sign_in_verification_code", placeHolderData);
|
||||||
|
|
||||||
// logger.info(`Login OTP email triggered for: ${sanitizeForLog(registered_email)}`);
|
logger.info(`Login OTP email triggered for: ${sanitizeForLog(registered_email)}`);
|
||||||
|
|
||||||
// return res.status(200).json({
|
return res.status(200).json({
|
||||||
// status: "success",
|
status: "success",
|
||||||
// message: "OTP sent successfully to your registered email",
|
message: "OTP sent successfully to your registered email",
|
||||||
// });
|
});
|
||||||
|
|
||||||
// } catch (err) {
|
} catch (err) {
|
||||||
// logger.error(`OTP request failed: ${err.message}`);
|
logger.error(`OTP request failed: ${err.message}`);
|
||||||
// logger.error(err.stack);
|
logger.error(err.stack);
|
||||||
|
|
||||||
// return res.status(500).json({
|
return res.status(500).json({
|
||||||
// status: "failed",
|
status: "failed",
|
||||||
// message: "Internal Server Error",
|
message: "Internal Server Error",
|
||||||
// });
|
});
|
||||||
// }
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.verifyOTPForLogin = async (req, res) => {
|
exports.verifyOTPForLogin = async (req, res) => {
|
||||||
// try {
|
try {
|
||||||
// const { registered_email, otp } = req.body;
|
const { registered_email, otp } = req.body;
|
||||||
|
|
||||||
// if (!registered_email || !otp) {
|
if (!registered_email || !otp) {
|
||||||
// return res.status(400).json({
|
return res.status(400).json({
|
||||||
// status: "failed",
|
status: "failed",
|
||||||
// message: "Email and OTP are required"
|
message: "Email and OTP are required"
|
||||||
// });
|
});
|
||||||
// }
|
}
|
||||||
|
|
||||||
// // Find user from either model
|
// Find user from either model
|
||||||
// let user = await EstablishmentUser.scope("withSensitive").findOne({
|
let user = await EstablishmentUser.scope("withSensitive").findOne({
|
||||||
// where: { email: registered_email }
|
where: { email: registered_email }
|
||||||
// });
|
});
|
||||||
|
|
||||||
// if (!user) {
|
if (!user) {
|
||||||
// user = await User.scope("withSensitive").findOne({
|
user = await User.scope("withSensitive").findOne({
|
||||||
// where: { email: registered_email, is_active: true }
|
where: { email: registered_email, is_active: true }
|
||||||
// });
|
});
|
||||||
// }
|
}
|
||||||
|
|
||||||
// // User validation
|
// User validation
|
||||||
// if (!user) {
|
if (!user) {
|
||||||
// return res.status(404).json({
|
return res.status(404).json({
|
||||||
// status: "failed",
|
status: "failed",
|
||||||
// message: "User not found"
|
message: "User not found"
|
||||||
// });
|
});
|
||||||
// }
|
}
|
||||||
|
|
||||||
// if (!user.login_otp) {
|
if (!user.login_otp) {
|
||||||
// return res.status(404).json({
|
return res.status(404).json({
|
||||||
// status: "failed",
|
status: "failed",
|
||||||
// message: "Verification code not found or invalid user"
|
message: "Verification code not found or invalid user"
|
||||||
// });
|
});
|
||||||
// }
|
}
|
||||||
|
|
||||||
// // Check OTP expiry
|
// Check OTP expiry
|
||||||
// if (new Date() > new Date(user.login_otp_expires_at)) {
|
if (new Date() > new Date(user.login_otp_expires_at)) {
|
||||||
// return res.status(400).json({
|
return res.status(400).json({
|
||||||
// status: "failed",
|
status: "failed",
|
||||||
// message: "Verification code has expired. Please request a new one"
|
message: "Verification code has expired. Please request a new one"
|
||||||
// });
|
});
|
||||||
// }
|
}
|
||||||
|
|
||||||
// // Verify OTP
|
// Verify OTP
|
||||||
// const isOtpValid = await bcrypt.compare(otp, user.login_otp);
|
const isOtpValid = await bcrypt.compare(otp, user.login_otp);
|
||||||
// if (!isOtpValid) {
|
if (!isOtpValid) {
|
||||||
// return res.status(400).json({
|
return res.status(400).json({
|
||||||
// status: "failed",
|
status: "failed",
|
||||||
// message: "Invalid verification code"
|
message: "Invalid verification code"
|
||||||
// });
|
});
|
||||||
// }
|
}
|
||||||
|
|
||||||
// // Clear OTP after successful verification
|
// Clear OTP after successful verification
|
||||||
// await user.update({
|
await user.update({
|
||||||
// login_otp: null,
|
login_otp: null,
|
||||||
// login_otp_expires_at: null,
|
login_otp_expires_at: null,
|
||||||
// });
|
});
|
||||||
|
|
||||||
// const safeEmail = sanitizeForLog(registered_email);
|
const safeEmail = sanitizeForLog(registered_email);
|
||||||
// logger.info(`OTP verification successful for user: ${safeEmail}`);
|
logger.info(`OTP verification successful for user: ${safeEmail}`);
|
||||||
|
|
||||||
// return res.status(200).json({
|
return res.status(200).json({
|
||||||
// status: "success",
|
status: "success",
|
||||||
// message: "Email verified successfully!",
|
message: "Email verified successfully!",
|
||||||
// });
|
});
|
||||||
|
|
||||||
// } catch (err) {
|
} catch (err) {
|
||||||
// logger.error(`OTP verification error: ${err.message}`);
|
logger.error(`OTP verification error: ${err.message}`);
|
||||||
// logger.error(`Stack trace: ${err.stack}`);
|
logger.error(`Stack trace: ${err.stack}`);
|
||||||
// return res.status(500).json({
|
return res.status(500).json({
|
||||||
// status: "failed",
|
status: "failed",
|
||||||
// message: "Internal server error"
|
message: "Internal server error"
|
||||||
// });
|
});
|
||||||
// }
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const GENERIC_ERROR_MSG =
|
const GENERIC_ERROR_MSG =
|
||||||
|
|||||||
@ -73,6 +73,16 @@ module.exports = (sequelize, DataTypes) => {
|
|||||||
type: DataTypes.DATE,
|
type: DataTypes.DATE,
|
||||||
allowNull: true,
|
allowNull: true,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
login_otp: {
|
||||||
|
type: DataTypes.STRING,
|
||||||
|
allowNull: true,
|
||||||
|
},
|
||||||
|
|
||||||
|
login_otp_expires_at: {
|
||||||
|
type: DataTypes.DATE,
|
||||||
|
allowNull: true,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
// ============================================================
|
// ============================================================
|
||||||
@ -84,7 +94,7 @@ module.exports = (sequelize, DataTypes) => {
|
|||||||
// 1. Hide sensitive fields by default
|
// 1. Hide sensitive fields by default
|
||||||
defaultScope: {
|
defaultScope: {
|
||||||
attributes: {
|
attributes: {
|
||||||
exclude: ["password", "reset_otp", "reset_otp_expires_at"],
|
exclude: ["password", "reset_otp", "reset_otp_expires_at", "login_otp", "login_otp_expires_at"],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -92,7 +102,7 @@ module.exports = (sequelize, DataTypes) => {
|
|||||||
scopes: {
|
scopes: {
|
||||||
withSensitive: {
|
withSensitive: {
|
||||||
attributes: {
|
attributes: {
|
||||||
include: ["password", "reset_otp", "reset_otp_expires_at"],
|
include: ["password", "reset_otp", "reset_otp_expires_at", "login_otp", "login_otp_expires_at"],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -105,6 +115,8 @@ module.exports = (sequelize, DataTypes) => {
|
|||||||
delete values.password;
|
delete values.password;
|
||||||
delete values.reset_otp;
|
delete values.reset_otp;
|
||||||
delete values.reset_otp_expires_at;
|
delete values.reset_otp_expires_at;
|
||||||
|
delete values.login_otp;
|
||||||
|
delete values.login_otp_expires_at;
|
||||||
return values;
|
return values;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -32,6 +32,15 @@ module.exports = (sequelize, DataTypes) => {
|
|||||||
type: DataTypes.DATE,
|
type: DataTypes.DATE,
|
||||||
allowNull: true,
|
allowNull: true,
|
||||||
},
|
},
|
||||||
|
login_otp: {
|
||||||
|
type: DataTypes.STRING,
|
||||||
|
allowNull: true,
|
||||||
|
},
|
||||||
|
|
||||||
|
login_otp_expires_at: {
|
||||||
|
type: DataTypes.DATE,
|
||||||
|
allowNull: true,
|
||||||
|
},
|
||||||
is_active: {
|
is_active: {
|
||||||
type: DataTypes.BOOLEAN,
|
type: DataTypes.BOOLEAN,
|
||||||
defaultValue: true,
|
defaultValue: true,
|
||||||
@ -40,7 +49,7 @@ module.exports = (sequelize, DataTypes) => {
|
|||||||
{
|
{
|
||||||
// 1. EXCLUDE sensitive fields from all queries
|
// 1. EXCLUDE sensitive fields from all queries
|
||||||
defaultScope: {
|
defaultScope: {
|
||||||
attributes: { exclude: ["password", "reset_otp", "reset_otp_expires_at"] },
|
attributes: { exclude: ["password", "reset_otp", "reset_otp_expires_at", "login_otp", "login_otp_expires_at"] },
|
||||||
},
|
},
|
||||||
|
|
||||||
// -------------------------------------------------------------
|
// -------------------------------------------------------------
|
||||||
@ -50,7 +59,7 @@ module.exports = (sequelize, DataTypes) => {
|
|||||||
scopes: {
|
scopes: {
|
||||||
withSensitive: {
|
withSensitive: {
|
||||||
attributes: {
|
attributes: {
|
||||||
include: ["password", "reset_otp", "reset_otp_expires_at"],
|
include: ["password", "reset_otp", "reset_otp_expires_at", "login_otp", "login_otp_expires_at"],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user