-
Notifications
You must be signed in to change notification settings - Fork 1
/
resetPasswordController.js
64 lines (56 loc) · 2.1 KB
/
resetPasswordController.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const User = require("../models/userModel");
const ErrorHandler = require("../utils/errorhandler");
const catchAsyncErrors = require("../middleware/catchAsyncErrors");
const sendEmail = require("../utils/sendEmail");
const crypto = require("crypto");
// Check If user Exists
exports.forgotPassword = catchAsyncErrors(async (req, res, next) => {
const { Email, FlatNo } = req.query;
let user1 = await User.find({ Email: Email, FlatNo: FlatNo });
if (!user1) {
return next(new ErrorHandler("You are not an existing user. Please register", 404));
}
const resetToken = user1[0].getResetPasswordToken();
user1[0].save();
console.log("in Schema : ", user1[0].resetPasswordToken);
const resetPasswordUrl = `${req.protocol}://lodha-frontend.vercel.app/updatepassword/${resetToken}`;
const message = `Your password reset Link is :- \n\n ${resetPasswordUrl} \n\n Please Note that the link gets deactivated within 15 minutes \n\nIf you have not requested this email then, please ignore it.`;
console.log(message);
await sendEmail({
email: Email,
subject: "Forgot Password Link",
message: message
})
res.status(200).json({
success: true,
message: "Link sent to mail to Update Password"
})
});
// Update User Password
exports.updatePassword = catchAsyncErrors(async (req, res, next) => {
const resetToken =crypto
.createHash("sha256")
.update(req.query.token)
.digest("hex");
console.log("While Update: ", resetToken);
const user1 = await User.findOne({
resetPasswordToken: resetToken,
resetPasswordExpire: {$gt : Date.now()}
})
console.log("user1: ", user1);
if (!user1 || user1.length==0) {
res.status(200).json({
success: false,
message: "password updation failed"
})
return
}
user1.Password = req.query.Password;
user1.resetPasswordToken = undefined;
user1.resetPasswordExpire = undefined;
user1.save();
res.status(200).json({
success: true,
message: "password updation successful"
})
});