Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public async changePassword(
userId: string,
oldPassword: string,
newPassword: string
): Promise {
if (!this.options.validatePassword(newPassword)) {
throw new Error(this.options.errors.invalidPassword);
}
const user = await this.passwordAuthenticator({ id: userId }, oldPassword);
const password = await bcryptPassword(newPassword);
await this.db.setPassword(userId, password);
this.server.getHooks().emit(ServerHooks.ChangePasswordSuccess, user);
if (this.options.invalidateAllSessionsAfterPasswordChanged) {
await this.db.invalidateAllSessions(user.id);
}
if (this.options.notifyUserAfterPasswordChanged) {
const address = user.emails && user.emails[0].address;
if (!address) {
throw new Error(this.options.errors.noEmailSet);
}
const passwordChangedMail = this.server.prepareMail(
address,
'',
this.server.sanitizeUser(user),
'',