Skip to content
Open
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
74 changes: 71 additions & 3 deletions Backend/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ const { hashingPassword, comparePassword } = require('../utils/HashingPassword')
const { generatingAuthToken } = require('../utils/generatingAuthToken')
const Review = require('../models/reviewModel')
const Blog = require('../models/blogModel')
const {generateOTP} = require('../utils/otp-services')

const {sendMail}= require('../utils/otp-services')

const registerUser = async (req, res, next) => {
try {
Expand Down Expand Up @@ -111,6 +114,68 @@ const loginUser = async (req, res, next) => {
}
}


const forgetPassword = async (req, res, next) => {
try {
const { email } = req.body;
if (!email) {
return res.status(400).send("Email is required");
}

const user = await User.findOne({ email });
if (!user) {
return res.status(404).send("User not found");
}

const otp = await generateOTP(email);

await sendMail(email,otp);
// Save the OTP and its expiration time in the user document
user.resetPasswordOTP = otp;
user.resetPasswordOTPExpires = Date.now() + 600000; // OTP expires in 10 minutes
await user.save();

res.json({
success: true,
message: "OTP sent to your email",
});
} catch (error) {
next(error);
}
};

const resetPassword = async (req, res, next) => {
try {
const { email, otp, newPassword } = req.body;
if (!email || !otp || !newPassword) {
return res.status(400).send("All input fields are required");
}

const user = await User.findOne({ email });
if (!user) {
return res.status(404).send("User not found");
}

if (user.resetPasswordOTP !== parseInt(otp) || user.resetPasswordOTPExpires < Date.now()) {
return res.status(400).send("Invalid OTP");
}

user.password = hashingPassword(newPassword);
user.resetPasswordOTP = undefined;
user.resetPasswordOTPExpires = undefined;
await user.save();

res.json({
success: true,
message: "Password reset successful",
});
} catch (error) {
next(error);
}
};



const updateUserProfile = async (req, res, next) => {
try {
const user = User.findOneAndUpdate(req.user._id)
Expand Down Expand Up @@ -262,7 +327,6 @@ const deletingComment = async (req, res, next) => {
}
}


module.exports = {
registerUser,
loginUser,
Expand All @@ -271,5 +335,9 @@ module.exports = {
writeReview,
likePost,
likeComment,
deletingComment
}
deletingComment,
forgetPassword,
resetPassword,
};


8 changes: 7 additions & 1 deletion Backend/models/userModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ const userSchema = new Schema({
type: Boolean,
required: true,
default: false,
}
},
resetPasswordOTP: {
type: Number,
},
resetPasswordOTPExpires: {
type: Date,
},
}, {
timestamps: true
})
Expand Down
Loading