Skip to content

Instantly share code, notes, and snippets.

@anandundavia
Last active April 11, 2018 06:17
Show Gist options
  • Save anandundavia/03ad019ea013af2b087b5f4538ff5b4e to your computer and use it in GitHub Desktop.
Save anandundavia/03ad019ea013af2b087b5f4538ff5b4e to your computer and use it in GitHub Desktop.
function changePassword(email, oldPassword, newPassword) {
var user = getUser(email);
var oldPasswordDecrypted = decrypt(oldPassword)
var newPasswordDecrypted = decrypt(newPassword)
var existingPasswordDecrypted = decrypt(user.password)
if (user === null || existingPasswordDecrypted !== oldPasswordDecrypted) {
throw new EmailPasswordMissMatchException();
}
if (oldPasswordDecrypted === newPasswordDecrypted) {
throw new PasswordAlreadyInUseException();
}
if (user.isAdmin) {
var isError = false;
try {
user.password = newPassword;
save(user);
// clear the session when the user changes the password
// so that he/she has to log in again
clearSession(user);
} catch (e) {
isError = true;
// throw something went wrong, try again
} finally {
if (!isError) {
var emails = user.getEmails();
var token = generatePasswordRevertToken(oldPassword, newPassword);
for (i = 0; i < emails.length; i++) {
// Confirm with admin that he as in fact changed the password,
// if he has not, give him option to reset the password using token
sendConfirmationEmail(email[i], token);
}
}
}
} else {
if (user.isEnabled) {
try {
user.password = newPassword;
save(user);
// clear the session when the user changes the password
// so that he/she has to log in again
clearSession(user);
} catch (e) {
throw new SomethingWentWrongException();
}
} else {
throw new UnsupporedOperationException();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment