Skip to content

Commit

Permalink
Add reset password endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
hagopj13 committed Nov 17, 2019
1 parent a86073f commit 3b43f7f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/controllers/auth.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@ const forgotPassword = catchAsync(async (req, res) => {
res.status(httpStatus.NO_CONTENT).send();
});

const resetPassword = catchAsync(async (req, res) => {
await authService.resetPassword(req.query.token, req.body.password);
res.status(httpStatus.NO_CONTENT).send();
});

module.exports = {
register,
login,
refreshTokens,
forgotPassword,
resetPassword,
};
1 change: 1 addition & 0 deletions src/routes/v1/auth.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ router.post('/register', validate(authValidation.register), authController.regis
router.post('/login', validate(authValidation.login), authController.login);
router.post('/refresh-tokens', validate(authValidation.refreshTokens), authController.refreshTokens);
router.post('/forgot-password', validate(authValidation.forgotPassword), authController.forgotPassword);
router.post('/reset-password', validate(authValidation.resetPassword), authController.resetPassword);

module.exports = router;
14 changes: 14 additions & 0 deletions src/services/auth.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const httpStatus = require('http-status');
const config = require('../config/config');
const tokenService = require('./token.service');
const userService = require('./user.service');
const Token = require('../models/token.model');
const AppError = require('../utils/AppError');

const generateAuthTokens = async userId => {
Expand Down Expand Up @@ -63,9 +64,22 @@ const generateResetPasswordToken = async email => {
return resetPasswordToken;
};

const resetPassword = async (resetPasswordToken, newPassword) => {
let userId;
try {
const resetPasswordTokenDoc = await tokenService.verifyToken(resetPasswordToken, 'resetPassword');
userId = resetPasswordTokenDoc.user;
await userService.updateUser(userId, { password: newPassword });
} catch (error) {
throw new AppError(httpStatus.UNAUTHORIZED, 'Password reset failed');
}
await Token.deleteMany({ user: userId, type: 'resetPassword' });
};

module.exports = {
generateAuthTokens,
loginUser,
refreshAuthTokens,
generateResetPasswordToken,
resetPassword,
};
12 changes: 12 additions & 0 deletions src/validations/auth.validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,21 @@ const forgotPassword = {
}),
};

const resetPassword = {
query: Joi.object().keys({
token: Joi.string().required(),
}),
body: Joi.object().keys({
password: Joi.string()
.required()
.custom(password),
}),
};

module.exports = {
register,
login,
refreshTokens,
forgotPassword,
resetPassword,
};

0 comments on commit 3b43f7f

Please sign in to comment.