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

Handle uncaught error sending email during registration and emit event. #84

Merged
merged 2 commits into from
Jul 8, 2024
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
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
Loading