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

Add 'useCustomMailer' and 'sendExistingUserEmail' config options #85

Merged
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
4 changes: 4 additions & 0 deletions config.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const exampleConfig = {
sendPasswordChangedEmail: true,
// If this is set, the user will be redirected to this location after confirming email instead of JSON response
confirmEmailRedirectURL: '/',
// Send an email if the user tried to signup with an existing email
sendExistingUserEmail: true,
// Set this to true to disable usernames and use emails instead
emailUsername: false,
// Also return the username and UUID when creating a session
Expand Down Expand Up @@ -102,6 +104,8 @@ const exampleConfig = {
couchAuthOnCloudant: false
},
mailer: {
// If you want to use the built in mailer or a custom one. If you want to use the custom one, you need to listen to the emitted events
useCustomMailer: false,
// Email address that all your system emails will be from
fromEmail: '[email protected]',
// Use this if you want to specify a custom Nodemailer transport. Defaults to SMTP or sendmail.
Expand Down
3 changes: 2 additions & 1 deletion src/config/default.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export const defaultConfig: Config = {
requireEmailConfirm: true,
sendConfirmEmail: true,
requirePasswordOnEmailChange: true,
sendPasswordChangedEmail: true
sendPasswordChangedEmail: true,
sendExistingUserEmail: true,
},
dbServer: {
protocol: 'http://',
Expand Down
4 changes: 4 additions & 0 deletions src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ export interface LocalConfig {
* confirming email instead of JSON response
*/
confirmEmailRedirectURL?: string;
/** Send an email if the user tried to signup with an existing email */
sendExistingUserEmail?: true
/** allow to also login with the username. Default: `false` */
usernameLogin: boolean;
/** allow to also login with the UUID. Default: `false` */
Expand Down Expand Up @@ -282,6 +284,8 @@ export interface RetryMailOptions {

/** Configure how [nodemailer](https://nodemailer.com/about/) sends mails. */
export interface MailerConfig {
/** If you want to use the built in mailer or a custom one. If you want to use the custom one, you need to listen to the emitted events */
useCustomMailer?: boolean;
/** Email address that all your system emails will be from */
fromEmail: string | Address;
/**
Expand Down
56 changes: 32 additions & 24 deletions src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,12 @@ export class User {

private async handleEmailExists(email: string, req?): Promise<void> {
const existingUser = await this.userDbManager.getUserBy('email', email);
await this.mailer.sendEmail('signupExistingEmail', email, {
user: existingUser,
req
});
if (this.config.local.sendExistingUserEmail && !this.config.mailer.useCustomMailer) {
await this.mailer.sendEmail('signupExistingEmail', email, {
user: existingUser,
req
});
}
this.emitter.emit('signup-attempt', existingUser, 'local');
}

Expand Down Expand Up @@ -471,7 +473,7 @@ export class User {
);
const result = await this.userDB.insert(finalNewUser);
newUser._rev = result.rev;
if (this.config.local.sendConfirmEmail) {
if (this.config.local.sendConfirmEmail && !this.config.mailer.useCustomMailer) {
ikosta marked this conversation as resolved.
Show resolved Hide resolved
await this.mailer.sendEmail(
'confirmEmail',
newUser.unverifiedEmail.email,
Expand Down Expand Up @@ -897,11 +899,13 @@ export class User {
status: 404
};
}
await this.mailer.sendEmail(
'forgotUsername',
user.email || user.unverifiedEmail.email,
{ user: user, req: req }
);
if (!this.config.mailer.useCustomMailer) {
await this.mailer.sendEmail(
'forgotUsername',
user.email || user.unverifiedEmail.email,
{ user: user, req: req }
);
}
this.emitter.emit('forgot-username', user);
} catch (err) {
this.emitter.emit('forgot-username-attempt', email);
Expand Down Expand Up @@ -956,7 +960,7 @@ export class User {
}

private async sendModifiedPasswordEmail(user: SlUserDoc, req): Promise<void> {
if (this.config.local.sendPasswordChangedEmail) {
if (this.config.local.sendPasswordChangedEmail && !this.config.mailer.useCustomMailer) {
ikosta marked this conversation as resolved.
Show resolved Hide resolved
await this.mailer.sendEmail(
'modifiedPassword',
user.email || user.unverifiedEmail.email,
Expand Down Expand Up @@ -1013,11 +1017,13 @@ export class User {
};
user = this.userDbManager.logActivity('forgot-password', 'local', user);
await this.userDB.insert(user);
await this.mailer.sendEmail(
'forgotPassword',
user.email || user.unverifiedEmail.email,
{ user: user, req: req, token: token }
);
if (!this.config.mailer.useCustomMailer) {
await this.mailer.sendEmail(
'forgotPassword',
user.email || user.unverifiedEmail.email,
{ user: user, req: req, token: token }
);
}
this.emitter.emit('forgot-password', user);
}

Expand Down Expand Up @@ -1088,14 +1094,16 @@ export class User {
email: newEmail,
token: URLSafeUUID()
};
await this.mailer.sendEmail(
'confirmEmailChange',
user.unverifiedEmail.email,
{
req: req,
user: user
}
);
if (!this.config.mailer.useCustomMailer) {
ikosta marked this conversation as resolved.
Show resolved Hide resolved
await this.mailer.sendEmail(
'confirmEmailChange',
user.unverifiedEmail.email,
{
req: req,
user: user
}
);
}
} else {
user.email = newEmail;
}
Expand Down
Loading