Skip to main content

IEmailChangedHook

Hook interface for executing actions after email address changes.

Overview​

The IEmailChangedHook interface enables reactions to email change events. Typically triggers TWO emails for security: one to the old address (alert) and one to the new address (confirmation).

The hook is non-blocking - errors are logged but do not affect the email change operation.

Interface​

interface IEmailChangedHook {
execute(metadata: EmailChangedMetadata): Promise<void>;
}

Metadata​

EmailChangedMetadata​

interface EmailChangedMetadata {
user: IUser;
oldEmail: string;
newEmail: string;
updateSource: UserProfileUpdateSource;
deactivatedMFADevices?: number;
clientInfo?: ClientInfo;
}
PropertyTypeDescription
userIUserUser whose email changed
oldEmailstringOld email address (before change)
newEmailstringNew email address (after change)
updateSourceUserProfileUpdateSourceSource of the email change
deactivatedMFADevicesnumberMFA devices deactivated due to change
clientInfoClientInfoIP, user agent, location

When Hook Fires​

  • User changes email via updateUserAttributes()

Example​

import { IEmailChangedHook, EmailChangedMetadata } from '@nauth-toolkit/core';

export class EmailChangedNotificationHook implements IEmailChangedHook {
async execute(metadata: EmailChangedMetadata): Promise<void> {
// Alert to old email
await this.emailService.sendEmailChangedAlertEmail({
to: metadata.oldEmail,
newEmail: metadata.newEmail,
});

// Confirmation to new email
await this.emailService.sendEmailChangedConfirmationEmail({
to: metadata.newEmail,
});
}
}