Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (this.server.options.ambiguousErrorMessages) {
return;
}
throw new Error(this.options.errors.userNotFound);
}
// Do not send an email if the address is already verified
const emailRecord = find(
user.emails,
(email: EmailRecord) => email.address.toLowerCase() === address.toLocaleLowerCase()
);
if (!emailRecord || emailRecord.verified) {
return;
}
const token = generateRandomToken();
await this.db.addEmailVerificationToken(user.id, address, token);
const resetPasswordMail = this.server.prepareMail(
address,
token,
this.server.sanitizeUser(user),
'verify-email',
this.server.options.emailTemplates.verifyEmail,
this.server.options.emailTemplates.from
);
await this.server.options.sendMail(resetPasswordMail);
}
public async sendResetPasswordEmail(address: string): Promise {
if (!address || !isString(address)) {
throw new Error(this.options.errors.invalidEmail);
}
const user = await this.db.findUserByEmail(address);
if (!user) {
// To prevent user enumeration we fail silently
if (this.server.options.ambiguousErrorMessages) {
return;
}
throw new Error(this.options.errors.userNotFound);
}
const token = generateRandomToken();
await this.db.addResetPasswordToken(user.id, address, token, 'reset');
const resetPasswordMail = this.server.prepareMail(
address,
token,
this.server.sanitizeUser(user),
'reset-password',
this.server.options.emailTemplates.resetPassword,
this.server.options.emailTemplates.from
);
await this.server.options.sendMail(resetPasswordMail);
}
public async sendEnrollmentEmail(address: string): Promise {
if (!address || !isString(address)) {
throw new Error(this.options.errors.invalidEmail);
}
const user = await this.db.findUserByEmail(address);
if (!user) {
throw new Error(this.options.errors.userNotFound);
}
const token = generateRandomToken();
await this.db.addResetPasswordToken(user.id, address, token, 'enroll');
const enrollmentMail = this.server.prepareMail(
address,
token,
this.server.sanitizeUser(user),
'enroll-account',
this.server.options.emailTemplates.enrollAccount,
this.server.options.emailTemplates.from
);
await this.server.options.sendMail(enrollmentMail);
}