Skip to content

Commit

Permalink
error handling, password reset
Browse files Browse the repository at this point in the history
  • Loading branch information
findthelorax committed Feb 22, 2024
1 parent 492d82f commit a7be265
Show file tree
Hide file tree
Showing 4 changed files with 286 additions and 7 deletions.
20 changes: 14 additions & 6 deletions controllers/authController.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,12 @@ exports.sendPasswordResetEmail = async (req, res, next) => {

const user = await models.User.findOne({ email });

if (!user) {
return next(new Error('User with this email does not exist'));
}
if (!user) {
const err = new Error('User with this email does not exist');
err.status = 400;
return next(err);
}


const token = Math.random().toString(36).substring(2);

Expand All @@ -109,7 +112,7 @@ exports.sendPasswordResetEmail = async (req, res, next) => {
const transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: process.env.SMTP_PORT,
secure: false, // true for 465, false for other ports
secure: true,
auth: {
user: process.env.EMAIL,
pass: process.env.EMAIL_PASSWORD,
Expand All @@ -118,7 +121,7 @@ exports.sendPasswordResetEmail = async (req, res, next) => {

// Send an email to the user with the password reset link
const mailOptions = {
from: '[email protected]',
from: process.env.EMAIL,
to: email,
subject: 'Password Reset',
text: `Click the following link to reset your password: ${IP}:${FPORT}/reset-password/${token}`,
Expand All @@ -136,6 +139,11 @@ exports.resetPassword = async (req, res, next) => {
const { token } = req.params;
const { password } = req.body;

const passwordStrength = zxcvbn(password);
if (passwordStrength.score < 3) {
return next(Object.assign(new Error('Password is too weak'), { status: 400 }));
}

const user = await models.User.findOne({
resetPasswordToken: token,
resetPasswordExpires: { $gt: Date.now() },
Expand All @@ -151,4 +159,4 @@ exports.resetPassword = async (req, res, next) => {
await user.save();

success(res, 'Password successfully reset');
};
};
263 changes: 263 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a7be265

Please sign in to comment.