forgot pw changes : GWM

This commit is contained in:
Gowtham M 2025-11-19 14:17:48 +05:30
parent 5dc4c953aa
commit bd020d4aef

View File

@ -730,34 +730,62 @@ exports.createRequest = async (req, res) => {
exports.forgotPasswordRequestOTP = async (req, res) => { exports.forgotPasswordRequestOTP = async (req, res) => {
try { try {
const { establishment_name, establishment_code, registered_email } = req.body; const { user_type , establishment_name, establishment_code, registered_email } = req.body;
// Validate inputs if(user_type == 'establishment_user')
if (!establishment_name || !establishment_code || !registered_email) {
return res.status(400).json({ status: "failed", message: "All fields are required" });
// Find establishment and user // Validate inputs
const establishment = await Establishment.findOne({ where: { establishment_code } }); if (!establishment_name || !establishment_code || !registered_email)
if (!establishment) return res.status(400).json({ status: "failed", message: "All fields are required" });
return res.status(404).json({ status: "failed", message: "Establishment not found" });
const user = await EstablishmentUser.findOne({ // Find establishment and user
where: { email: registered_email, establishment_id: establishment.id }, const establishment = await Establishment.findOne({ where: { establishment_code } });
}); if (!establishment)
if (!user) return res.status(404).json({ status: "failed", message: "Establishment not found" });
return res.status(404).json({ status: "failed", message: "User not found for this establishment" });
// Generate 6-digit OTP const user = await EstablishmentUser.findOne({
const otp = Math.floor(100000 + Math.random() * 900000).toString(); where: { email: registered_email, establishment_id: establishment.id },
});
if (!user)
return res.status(404).json({ status: "failed", message: "User not found for this establishment" });
// Generate 6-digit OTP
const otp = Math.floor(100000 + Math.random() * 900000).toString();
// Hash OTP before saving (security)
const hashedOtp = await bcrypt.hash(otp, 10);
// Store OTP in user record
await user.update({
reset_otp: hashedOtp,
reset_otp_expires_at: new Date(Date.now() + 10 * 60 * 1000), // valid for 10 mins
});
}else{
const adminUser = await user.findOne({where: { email: registered_email },});
if (!adminUser)
return res.status(404).json({ status: "failed", message: "Admin user not found" });
// Generate 6-digit OTP
const otp = Math.floor(100000 + Math.random() * 900000).toString();
// Hash OTP before saving (security)
const hashedOtp = await bcrypt.hash(otp, 10);
// Store OTP in user record
await adminUser.update({
reset_otp: hashedOtp,
reset_otp_expires_at: new Date(Date.now() + 10 * 60 * 1000), // valid for 10 mins
});
}
// Hash OTP before saving (security)
const hashedOtp = await bcrypt.hash(otp, 10);
// Store OTP in user record
await user.update({
reset_otp: hashedOtp,
reset_otp_expires_at: new Date(Date.now() + 10 * 60 * 1000), // valid for 10 mins
});
// Send OTP email // Send OTP email
await sendEmail( await sendEmail(
@ -781,7 +809,8 @@ exports.forgotPasswordRequestOTP = async (req, res) => {
exports.forgotPasswordVerifyOTP = async (req, res) => { exports.forgotPasswordVerifyOTP = async (req, res) => {
try { try {
const { registered_email, otp, password, confirm_password } = req.body; const { user_type , registered_email, otp, password, confirm_password } = req.body;
if (!registered_email || !otp || !password || !confirm_password) if (!registered_email || !otp || !password || !confirm_password)
return res.status(400).json({ status: "failed", message: "All fields are required" }); return res.status(400).json({ status: "failed", message: "All fields are required" });
@ -789,26 +818,57 @@ exports.forgotPasswordVerifyOTP = async (req, res) => {
if (password !== confirm_password) if (password !== confirm_password)
return res.status(400).json({ status: "failed", message: "Passwords do not match" }); return res.status(400).json({ status: "failed", message: "Passwords do not match" });
const user = await EstablishmentUser.findOne({ where: { email: registered_email } });
if (!user || !user.reset_otp)
return res.status(404).json({ status: "failed", message: "verification code not found or invalid user" });
// Check OTP expiry if(user_type == 'establishment_user')
if (new Date() > new Date(user.reset_otp_expires_at)) {
return res.status(400).json({ status: "failed", message: "Verification code has expired. Please request a new one" });
// Compare OTP const user = await EstablishmentUser.findOne({ where: { email: registered_email } });
const isOtpValid = await bcrypt.compare(otp, user.reset_otp); if (!user || !user.reset_otp)
if (!isOtpValid) return res.status(404).json({ status: "failed", message: "verification code not found or invalid user" });
return res.status(400).json({ status: "failed", message: "Invalid verification code" });
// Update password // Check OTP expiry
const hashedPassword = await bcrypt.hash(password, 10); if (new Date() > new Date(user.reset_otp_expires_at))
await user.update({ return res.status(400).json({ status: "failed", message: "Verification code has expired. Please request a new one" });
password: hashedPassword,
reset_otp: null, // Compare OTP
reset_otp_expires_at: null, const isOtpValid = await bcrypt.compare(otp, user.reset_otp);
}); if (!isOtpValid)
return res.status(400).json({ status: "failed", message: "Invalid verification code" });
// Update password
const hashedPassword = await bcrypt.hash(password, 10);
await user.update({
password: hashedPassword,
reset_otp: null,
reset_otp_expires_at: null,
});
}else{
const adminUser = await user.findOne({ where: { email: registered_email } });
if (!adminUser || !adminUser.reset_otp)
return res.status(404).json({ status: "failed", message: "verification code not found or invalid user" });
// Check OTP expiry
if (new Date() > new Date(adminUser.reset_otp_expires_at))
return res.status(400).json({ status: "failed", message: "Verification code has expired. Please request a new one" });
// Compare OTP
const isOtpValid = await bcrypt.compare(otp, adminUser.reset_otp);
if (!isOtpValid)
return res.status(400).json({ status: "failed", message: "Invalid verification code" });
// Update password
const hashedPassword = await bcrypt.hash(password, 10);
await adminUser.update({
password: hashedPassword,
reset_otp: null,
reset_otp_expires_at: null,
});
}
logger.info(`Password reset successful for user=${registered_email}`); logger.info(`Password reset successful for user=${registered_email}`);