Skip to content

Commit

Permalink
Reset-Password-Link
Browse files Browse the repository at this point in the history
  • Loading branch information
sivm99 committed Oct 6, 2024
1 parent b5133a9 commit 41db028
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
8 changes: 4 additions & 4 deletions Controller/User/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ export const getUser = asyncErrorHandler(async (req, res, next) => {
});

export const updatePassword = asyncErrorHandler(async (req, res, next) => {
const { currentPassword, newPassword, newPasswordConfirm } = req.body;
if (!currentPassword || !newPassword || !newPasswordConfirm) {
const { currentPassword, password, passwordConfirm } = req.body;
if (!currentPassword || !password || !passwordConfirm) {
return next(new CustomError("Please provide all the required fields", 400));
}
const user = await User.findById(req.user.id).select("+password");
if (!(await user.correctPassword(currentPassword, user.password))) {
return next(new CustomError("Your current password is wrong", 401));
}
user.password = newPassword;
user.passwordConfirm = newPasswordConfirm;
user.password = password;
user.passwordConfirm = passwordConfirm;
const updatedUser = await user.save({ validateBeforeSave: true });
const id = req.user.id || req.user._id || updatedUser.id || updatedUser._id;
const safeUser = {
Expand Down
25 changes: 18 additions & 7 deletions Controller/authController.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,35 +97,46 @@ export const forgetPassword = asyncErrorHandler(async (req, res, next) => {
if (!email) {
return next(new CustomError("Please provide your email", 400));
}

const user = await User.findOne({ email });
if (!user) {
return next(new CustomError("There is no user with that email", 404));
}

const resetToken = await user.createPasswordResetToken();
await user.save({ validateBeforeSave: false });

const resetURL = `${req.protocol}://${req.get(
"host"
)}/api/v1/auth/resetPassword/${resetToken}`;
const message = `Forgot your password?
Submit a PATCH request with your new password and passwordConfirm to: ${resetURL}.\nIf you didn't forget your password, please ignore this email!`;
)}/auth/reset-password/${resetToken}`;

const message = `Forgot your password?
Click on the given link to reset your password: ${resetURL}.
If you didn't forget your password, please ignore this email!
With love,
One Alias Services.`;

try {
await sendEmail({
email: user.email,
subject: "Your password reset token (valid for 10 min)",
message,
html: htmlMessage,
});

res.status(200).json({
status: "success",
message: "Token sent to email!",
message: `Reset Link sent to ${user.email}`,
});
} catch (err) {
user.passwordResetToken = undefined;
user.passwordResetExpires = undefined;
await user.save({ validateBeforeSave: false });

return next(
new CustomError("There was an error sending the email. Try again later!"),
500
new CustomError(
"There was an error sending the email. Try again later!",
500
)
);
}
});
Expand Down
3 changes: 3 additions & 0 deletions Models/userModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const userSchema = new mongoose.Schema({
username: {
type: String,
minlength: [4, "Username must be at least 4 characters"],
maxlength: [15, "Username must be at most 15 characters"],
required: [true, "Please provide your username"],
unique: [true, "Username already taken"],
lowercase: true,
Expand All @@ -17,6 +18,8 @@ const userSchema = new mongoose.Schema({
},
name: {
type: String,
minlength: [4, "Name must be at least 4 characters"],
maxlength: [64, "Name must be at most 64 characters"],
required: [true, "Please provide your name"],
},
email: {
Expand Down
4 changes: 2 additions & 2 deletions utils/safeResponseObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
* @returns {Object} The transformed rule object with the properties alias, destination, username, ruleId, name, and active.
*/
export const sendRule = (rule) => {
const { alias, destination, username, name, id, _id, active } = rule;
const { alias, destination, username, name, id, _id, enabled } = rule;
const ruleId = id || _id;
return { alias, destination, username, ruleId, name, active };
return { aliasEmail: alias, destinationEmail: destination, username, ruleId, name, active:enabled };
};

/**
Expand Down

0 comments on commit 41db028

Please sign in to comment.