Skip to content

Commit

Permalink
Handle errors during sending confirm email
Browse files Browse the repository at this point in the history
During user creation, an exception while sending the confirmation email would hang express. Wrap the email confirmation sending in try/catch. Emit event 'confirmation-email-error' if mail fails to send. (#84)
  • Loading branch information
mugwhump authored Jul 8, 2024
1 parent 969f5a3 commit fbb800f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ Here is a full list of the events that CouchAuth emits, and parameters provided:
- `logout`: (`user_id`)
- `logout-all`: (`user_id`)
- `consents`: (`userDoc`)
- `confirmation-email-error`: (`userDoc`)

## Main API

Expand Down
3 changes: 2 additions & 1 deletion src/types/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ export type UserEvent =
| 'forgot-username-attempt'
| 'email-change-attempt'
| 'user-db-added'
| 'user-deleted';
| 'user-deleted'
| 'confirmation-email-error';

export interface UserActivity {
timestamp: string;
Expand Down
23 changes: 15 additions & 8 deletions src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ export class User {
}
}

// TODO: This is an instance of the promise constructor anti-pattern
return new Promise(async (resolve, reject) => {
newUser = await this.prepareNewUser(newUser);
if (hasError || !this.config.security.loginOnRegistration) {
Expand Down Expand Up @@ -474,14 +475,20 @@ export class User {
const result = await this.userDB.insert(finalNewUser);
newUser._rev = result.rev;
if (this.config.local.sendConfirmEmail && !this.config.mailer.useCustomMailer) {
await this.mailer.sendEmail(
'confirmEmail',
newUser.unverifiedEmail.email,
{
req: req,
user: newUser
}
);
try {
await this.mailer.sendEmail(
'confirmEmail',
newUser.unverifiedEmail.email,
{
req: req,
user: newUser
}
);
}
catch (err) {
this.emitter.emit('confirmation-email-error', newUser);
console.warn('error sending confirmation email to '+newUser.unverifiedEmail?.email, err);
}
}
return newUser as SlUserDoc;
}
Expand Down

0 comments on commit fbb800f

Please sign in to comment.