Skip to content

Commit

Permalink
bug-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bertrandshema committed May 15, 2024
2 parents 35ab07f + 60db855 commit 46632ba
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/controller/userController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,75 @@ export const verify2FA = errorHandler(async (req: Request, res: Response) => {
});


export const recoverPassword = async (req: Request, res: Response) => {
try {
const { email } = req.body as { email: string };

const user = await userRepository.findOne({ where: { email } });

if (!user) {
return res.status(404).json({ message: 'User not found' });
}

// Generate a JWT token with the user's email as the payload
const recoverToken = jwt.sign({ email : user.email }, process.env.JWT_SECRET as jwt.Secret, { expiresIn: '1h' });

const confirmLink = `${process.env.APP_URL}/api/v1/recover/confirm?recoverToken=${recoverToken}`;
await sendEmail('confirmPassword', email, { name: user.firstName, link: confirmLink });

return res.status(200).json({ message: 'Password reset token generated successfully', recoverToken });

} catch (error) {
return res.status(500).json({ message: 'Internal server error' });
}
};

//password Recover Confirmation
export const updateNewPassword = async (req: Request, res: Response) => {
const recoverToken = req.query.recoverToken as string;

const { password } = req.body as { password: string };

if (!recoverToken) {
return res.status(404).json({ message: 'Token is required' });
}

try {
const decoded = jwt.verify(recoverToken, process.env.JWT_SECRET as jwt.Secret) as {
email : string;
};
const user = await userRepository.findOne({
where: { email: decoded.email },
});

if (!user) {
return res.status(404).json({ message: 'User not found' });
}

const hashedPassword : string = await bcrypt.hash(password, 10);
user.password = hashedPassword;

await userRepository.save(user);

return res.status(200).json({ message: 'Password updated successfully' });

} catch (error) {
// Check if the error is an instance of Error
if (error instanceof Error) {
// Check if the error is related to token verification failure
if (error.name === 'TokenExpiredError' || error.name === 'JsonWebTokenError') {
return res.status(404).json({ message: 'Invalid or expired token' });
}
}
// If it's not a token verification error or not an instance of Error, handle other errors with a 500 response
return res.status(500).json({ message: 'Internal Server Error' });
}
};

<<<<<<< HEAD

=======

export const recoverPassword = async (req: Request, res: Response) => {
try {
const { email } = req.body as { email: string };
Expand Down Expand Up @@ -294,3 +363,4 @@ export const updateNewPassword = async (req: Request, res: Response) => {
}


>>>>>>> 60db8552cfa192cd75df91196e06ca2d5700bfa8
14 changes: 14 additions & 0 deletions src/middlewares/errorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,24 @@ type MiddlewareFunction = (req: Request, res: Response) => Promise<Response<Reco
function errorHandler(func: MiddlewareFunction): MiddlewareFunction {
return async (req: Request, res: Response) => {
try {
<<<<<<< HEAD
return await func(req, res);
} catch (error) {
const message = (error as { detail?: string }).detail || 'Internal Server Error';
return res.status(500).send(message);
=======
await func(req, res, next);
} catch (error) {
// Check if the error is an instance of Error
if (error instanceof Error) {
// Check if the error is related to token verification failure
if (error.name === 'TokenExpiredError' || error.name === 'JsonWebTokenError') {
return res.status(404).json({ message: 'Invalid or expired token' });
}
}
// If it's not a token verification error or not an instance of Error, handle other errors with a 500 response
return res.status(500).json({ message: 'Internal Server Error' });
>>>>>>> 60db8552cfa192cd75df91196e06ca2d5700bfa8
}
};
}
Expand Down

0 comments on commit 46632ba

Please sign in to comment.