Skip to content

Commit c756101

Browse files
committed
WIP change password route
1 parent 1eb3e79 commit c756101

3 files changed

Lines changed: 38 additions & 5 deletions

File tree

middlewares/auth.middleware.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,26 @@ async function retrieveRoleBindings(req, res, next) {
125125
return next();
126126
}
127127

128+
/**
129+
*
130+
* @param {*} req
131+
* @param {*} res
132+
* @param {*} next
133+
*/
134+
async function changePassword(req, res, next) {
135+
acc = await Services.Account.getAccountIfValid(req.user.email, req.body.oldPassword);
136+
// user's old password is correct
137+
if (!!acc) {
138+
req.body.account = await Services.Account.updatePassword(req.user.id, req.body.newPassword);
139+
return next();
140+
} else {
141+
return next({
142+
status: 401,
143+
message: Constants.Error.AUTH_401_MESSAGE,
144+
});
145+
}
146+
}
147+
128148
/**
129149
* Middleware that sends an email to reset the password for the inputted email address.
130150
* @param {{body: {email:String}}} req the request object
@@ -428,5 +448,6 @@ module.exports = {
428448
addCreationRoleBindings: Middleware.Util.asyncMiddleware(addCreationRoleBindings),
429449
resendConfirmAccountEmail: Middleware.Util.asyncMiddleware(resendConfirmAccountEmail),
430450
retrieveRoleBindings: Middleware.Util.asyncMiddleware(retrieveRoleBindings),
431-
retrieveRoles: Middleware.Util.asyncMiddleware(retrieveRoles)
451+
retrieveRoles: Middleware.Util.asyncMiddleware(retrieveRoles),
452+
changePassword: Middleware.Util.asyncMiddleware(changePassword),
432453
};

middlewares/validators/auth.validator.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ module.exports = {
55
ForgotPasswordValidator: [
66
VALIDATOR.emailValidator("body", "email", false)
77
],
8+
ChangePasswordValidator: [
9+
VALIDATOR.passwordValidator("body", "oldPassword", false),
10+
VALIDATOR.passwordValidator("body", "newPassword", false)
11+
],
812
ResetPasswordValidator: [
9-
VALIDATOR.passwordValidator("body","password", false),
13+
VALIDATOR.passwordValidator("body", "password", false),
1014
//The json web token is provided via the header with param "Authentication".
11-
VALIDATOR.jwtValidator("header","X-Reset-Token", process.env.JWT_RESET_PWD_SECRET, false)
15+
VALIDATOR.jwtValidator("header", "X-Reset-Token", process.env.JWT_RESET_PWD_SECRET, false)
1216
],
1317
accountConfirmationValidator: [
14-
VALIDATOR.jwtValidator("param","token", process.env.JWT_CONFIRM_ACC_SECRET, false)
18+
VALIDATOR.jwtValidator("param", "token", process.env.JWT_CONFIRM_ACC_SECRET, false)
1519
]
16-
};
20+
};

routes/api/auth.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ module.exports = {
9797
Controllers.Auth.sentResetEmail
9898
);
9999

100+
authRouter.coute("/password/change").post(
101+
Middleware.Validator.Auth.ChangePasswordValidator,
102+
Middleware.parseBody.middleware,
103+
104+
Middleware.Auth.changePassword,
105+
Controllers.Auth.resetPassword,
106+
);
107+
100108
//untested
101109
/**
102110
* @api {post} /auth/password/reset reset password

0 commit comments

Comments
 (0)