Skip to main content

EmailVerificationService

Package: @nauth-toolkit/core Type: Service

Email verification service that handles sending verification codes, verifying emails with codes or tokens, and resending with rate limiting.

import { EmailVerificationService } from '@nauth-toolkit/nestjs';

Overview

Handles email verification workflow including code generation, email delivery, verification with code or token, and resend operations with rate limiting.

note

Auto-injected by framework. No manual instantiation required.

Methods

resendVerificationEmail()

Resend verification email with rate limiting.

async resendVerificationEmail(dto: ResendVerificationEmailDTO): Promise<ResendVerificationEmailResponseDTO>

Parameters

  • dto - ResendVerificationEmailDTO - Request DTO
    • sub - string (optional) - User identifier (UUID v4)
    • email - string (optional) - User email address
    • baseUrl - string (optional) - Base URL for verification link
info

Either sub or email must be provided (not both).

Returns

Errors

CodeWhenDetails
NOT_FOUNDUser not found{ userId?: string }
RATE_LIMIT_RESENDResend delay not met{ retryAfter: number, resendDelay: number }
VALIDATION_FAILEDNeither sub nor email{ message: string }

Example

import { ResendVerificationEmailDTO } from '@nauth-toolkit/nestjs';

@Injectable()
export class MyService {
constructor(private emailVerificationService: EmailVerificationService) {}

async resendBySub() {
const dto: ResendVerificationEmailDTO = {
sub: 'a21b654c-2746-4168-acee-c175083a65cd',
baseUrl: 'https://example.com',
};
await this.emailVerificationService.resendVerificationEmail(dto);
}

async resendByEmail() {
const dto: ResendVerificationEmailDTO = {
email: 'user@example.com',
baseUrl: 'https://example.com',
};
await this.emailVerificationService.resendVerificationEmail(dto);
}
}

sendMFAEmailCode()

Send MFA email verification code. Used internally during MFA challenges.

async sendMFAEmailCode(dto: SendVerificationEmailDTO): Promise<SendVerificationEmailResponseDTO>

Parameters

  • dto - SendVerificationEmailDTO - Request DTO
    • sub - string - User identifier (UUID v4)
    • challengeSessionId - number (optional) - Challenge session ID to link the verification token to a specific session
    • skipAlreadyVerifiedCheck - boolean (optional) - Skip already verified check

Returns

Errors

CodeWhenDetails
ALREADY_VERIFIEDEmail already verified{}
NOT_FOUNDUser not found{ userId: string }
RATE_LIMIT_EMAILToo many requests{ retryAfter: number, currentCount: number }
RATE_LIMIT_RESENDResend delay not met{ retryAfter: number, resendDelay: number }

sendVerificationEmail()

Send verification email to user with code and optional link.

async sendVerificationEmail(dto: SendVerificationEmailDTO): Promise<SendVerificationEmailResponseDTO>

Parameters

  • dto - SendVerificationEmailDTO - Request DTO
    • sub - string - User identifier (UUID v4)
    • baseUrl - string (optional) - Base URL for verification link
    • skipAlreadyVerifiedCheck - boolean (optional) - Skip already verified check (for MFA)
    • challengeSessionId - number (optional) - Challenge session ID to link the verification token to a specific session
    • challengeSessionToken - string (optional) - Challenge session token (UUID v4) to embed in the verification link for cross-browser verification

Returns

Errors

CodeWhenDetails
ALREADY_VERIFIEDEmail already verified{}
NOT_FOUNDUser not found{ userId: string }
RATE_LIMIT_EMAILToo many requests{ retryAfter: number, currentCount: number }
RATE_LIMIT_RESENDResend delay not met{ retryAfter: number, resendDelay: number }

Example

import { SendVerificationEmailDTO } from '@nauth-toolkit/nestjs';

@Injectable()
export class MyService {
constructor(private emailVerificationService: EmailVerificationService) {}

async sendCode() {
const dto: SendVerificationEmailDTO = {
sub: 'a21b654c-2746-4168-acee-c175083a65cd',
baseUrl: 'https://example.com',
};
const result = await this.emailVerificationService.sendVerificationEmail(dto);
console.log('Token ID:', result.tokenId);
}
}

verifyEmailWithCode()

Verify email address using 6-digit code.

async verifyEmailWithCode(dto: VerifyEmailWithCodeDTO): Promise<VerifyEmailResponseDTO>

Parameters

  • dto - VerifyEmailWithCodeDTO - Request DTO
    • email - string - User email address
    • code - string - 6-digit verification code

Returns

Errors

CodeWhenDetails
NOT_FOUNDUser not found{ email: string }
VERIFICATION_CODE_EXPIREDCode expired{}
VERIFICATION_CODE_INVALIDInvalid or expired code{}
VERIFICATION_TOO_MANY_ATTEMPTSToo many attempts{}

Example

import { VerifyEmailWithCodeDTO } from '@nauth-toolkit/nestjs';

@Injectable()
export class MyService {
constructor(private emailVerificationService: EmailVerificationService) {}

async verify() {
const dto: VerifyEmailWithCodeDTO = {
email: 'user@example.com',
code: '123456',
};
const result = await this.emailVerificationService.verifyEmailWithCode(dto);
console.log(result.message);
}
}

verifyEmailWithToken()

Verify email address using URL token (link-based verification).

async verifyEmailWithToken(dto: VerifyEmailWithTokenDTO): Promise<VerifyEmailResponseDTO>

Parameters

Returns

Errors

CodeWhenDetails
VERIFICATION_CODE_EXPIREDToken expired{}
VERIFICATION_CODE_INVALIDInvalid or expired token{}

Example

import { VerifyEmailWithTokenDTO } from '@nauth-toolkit/nestjs';

@Injectable()
export class MyService {
constructor(private emailVerificationService: EmailVerificationService) {}

async verifyWithLink() {
const dto: VerifyEmailWithTokenDTO = {
token: 'abc123...', // 64-char hex string from URL
};
const result = await this.emailVerificationService.verifyEmailWithToken(dto);
console.log(result.message);
}
}