Skip to content

Commit

Permalink
Merge pull request #12 from ELEVATE-Project/development
Browse files Browse the repository at this point in the history
reset api changes
  • Loading branch information
rakeshSgr authored Apr 1, 2022
2 parents a328c35 + ccf2273 commit 4bf43b0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
3 changes: 2 additions & 1 deletion constants/api-responses.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ module.exports = {
"USERS_FETCHED_SUCCESSFULLY": "Users fetched successfully.",
"INVALID_SECRET_CODE": "Incorrect code. Please try again.",
"USER_ROLE_UPDATED": "You have been logged out of your account due to change in platform role. Please login again.",
"UNABLE_TO_SEND_OTP": "Unable to send otp, may be redis server is down."
"UNABLE_TO_SEND_OTP": "Unable to send otp, may be redis server is down.",
"RESET_PREVIOUS_PASSWORD": "Please enter a new password, that has not been used before"
};
10 changes: 9 additions & 1 deletion services/helper/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ module.exports = class AccountHelper {
*/

static async resetPassword(bodyData) {
const projection = { refreshTokens: 0, "designation.deleted": 0, "designation._id": 0, "areasOfExpertise.deleted": 0, "areasOfExpertise._id": 0, "location.deleted": 0, "location._id": 0, password: 0 };
const projection = { refreshTokens: 0, "designation.deleted": 0, "designation._id": 0, "areasOfExpertise.deleted": 0, "areasOfExpertise._id": 0, "location.deleted": 0, "location._id": 0 };
try {
let user = await usersData.findOne({ 'email.address': bodyData.email }, projection);
if (!user) {
Expand All @@ -392,6 +392,10 @@ module.exports = class AccountHelper {
if (!redisData || redisData.otp != bodyData.otp) {
return common.failureResponse({ message: apiResponses.RESET_OTP_INVALID, statusCode: httpStatusCode.bad_request, responseCode: 'CLIENT_ERROR' });
}
const isPasswordCorrect = bcryptJs.compareSync(bodyData.password, user.password);
if (isPasswordCorrect) {
return common.failureResponse({ message: apiResponses.RESET_PREVIOUS_PASSWORD, statusCode: httpStatusCode.bad_request, responseCode: 'CLIENT_ERROR' });
}

const salt = bcryptJs.genSaltSync(10);
bodyData.password = bcryptJs.hashSync(bodyData.password, salt);
Expand All @@ -405,6 +409,8 @@ module.exports = class AccountHelper {
}
};



const accessToken = utilsHelper.generateToken(tokenDetail, process.env.ACCESS_TOKEN_SECRET, '1d');
const refreshToken = utilsHelper.generateToken(tokenDetail, process.env.REFRESH_TOKEN_SECRET, '183d');

Expand All @@ -418,8 +424,10 @@ module.exports = class AccountHelper {
await usersData.updateOneUser({ _id: user._id }, updateParams);

/* Mongoose schema is in strict mode, so can not delete otpInfo directly */
delete user._doc.password;
user = { ...user._doc };
delete user.otpInfo;

const result = { access_token: accessToken, refresh_token: refreshToken, user };

return common.successResponse({ statusCode: httpStatusCode.ok, message: apiResponses.PASSWORD_RESET_SUCCESSFULLY, result });
Expand Down

0 comments on commit 4bf43b0

Please sign in to comment.