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

Update authController.js #243

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 9 additions & 5 deletions server/controllers/auth/authController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { generateOTP } from "../../utils/generateOTP.js";
import { sendEmail } from "../../utils/sendEmail.js";
import { generateToken } from "../../utils/generateJwtToken.js";
import { decodeJWT } from "../../utils/decodeJwtToken.js";
import bcrypt from 'bcrypt';
/*
Forgot Password

Expand All @@ -21,6 +22,7 @@ const forgotPassword = async (req, res) => {

// generating otp
const otp = generateOTP();
const otpExpiration = Date.now() + 2 * 60 * 1000; // OTP expires in 2 minutes
await OtpModel.create({ email: user.email, otp });
// Send OTP via email
const emailOptions = {
Expand Down Expand Up @@ -64,10 +66,11 @@ const verifyAndResetPassword = async (req, res) => {
const otpData = await OtpModel.findOne({ email }).sort({ createdAt: -1 });

// Check if OTP exists in the database
if (!otpData) {
return res
.status(404)
.json({ success: false, message: "OTP Expired. Click on Resend OTP" });
if (!otpData || otpData.expiresAt < Date.now()) {
return res.status(404).json({
success: false,
message: "OTP Expired. Click on Resend OTP"
});
}

// Compare the OTP provided by the user with the OTP stored in the database
Expand Down Expand Up @@ -157,7 +160,8 @@ const setNewPassword = async (req, res) => {
message: "Invalid Token or User not found",
});
}

// Hash the new password before saving
const hashedPassword = await bcrypt.hash(newPassword, 10);
user.password = newPassword;
user = await user.save();

Expand Down