Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reset api changes #12

Merged
merged 1 commit into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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