Skip to main content

PhoneVerificationService

Package: @nauth-toolkit/core Type: Service

Phone verification service that handles sending verification SMS codes, verifying phones with codes, and resending with rate limiting.

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

Overview

Handles phone verification workflow including code generation, SMS delivery, verification with code, and resend operations with rate limiting.

note

Auto-injected by framework. No manual instantiation required.

Methods

resendVerificationSMS()

Resend verification SMS with rate limiting. Supports identification by user sub or phone number.

async resendVerificationSMS(dto: ResendVerificationSMSDTO): Promise<ResendVerificationSMSResponseDTO>

Parameters

  • dto - ResendVerificationSMSDTO - Request DTO
    • sub - string (optional) - User identifier (UUID v4)
    • phone - string (optional) - User phone number (E.164 format)
info

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

Returns

Errors

CodeWhenDetails
ALREADY_VERIFIEDPhone already verified{}
NOT_FOUNDUser not found{ userId?: string }
PHONE_REQUIREDNo phone on account{}
RATE_LIMIT_RESENDResend delay not met{ retryAfter: number, resendDelay: number }
RATE_LIMIT_SMSToo many requests{ retryAfter: number, currentCount: number }
VALIDATION_FAILEDNeither sub nor phone{ message: string }

Example

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

@Injectable()
export class MyService {
constructor(private phoneVerificationService: PhoneVerificationService) {}

async resendBySub() {
const dto: ResendVerificationSMSDTO = {
sub: 'a21b654c-2746-4168-acee-c175083a65cd',
};
await this.phoneVerificationService.resendVerificationSMS(dto);
}

async resendByPhone() {
const dto: ResendVerificationSMSDTO = {
phone: '+1234567890',
};
await this.phoneVerificationService.resendVerificationSMS(dto);
}
}

sendVerificationSMS()

Send verification SMS to user with code.

async sendVerificationSMS(dto: SendVerificationSMSDTO): Promise<SendVerificationSMSResponseDTO>

Parameters

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

Returns

Errors

CodeWhenDetails
ALREADY_VERIFIEDPhone already verified{}
NOT_FOUNDUser not found{ userId: string }
PHONE_REQUIREDNo phone on account{}
RATE_LIMIT_RESENDResend delay not met{ retryAfter: number, resendDelay: number }
RATE_LIMIT_SMSToo many requests{ retryAfter: number, currentCount: number }

Example

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

@Injectable()
export class MyService {
constructor(private phoneVerificationService: PhoneVerificationService) {}

async sendCode() {
const dto: SendVerificationSMSDTO = {
sub: 'a21b654c-2746-4168-acee-c175083a65cd',
skipAlreadyVerifiedCheck: false,
};
const result = await this.phoneVerificationService.sendVerificationSMS(dto);
console.log('Token ID:', result.tokenId);
}
}

verifyPhoneWithCode()

Verify phone number using 6-digit code.

async verifyPhoneWithCode(dto: VerifyPhoneWithCodeDTO): Promise<VerifyPhoneResponseDTO>

Parameters

  • dto - VerifyPhoneWithCodeDTO - Request DTO
    • phone - string - Phone number (E.164 format)
    • code - string - 6-digit verification code
    • challengeSessionId - number (optional) - Restrict validation to a specific challenge session

Returns

Errors

CodeWhenDetails
VERIFICATION_CODE_EXPIREDCode expired{}
VERIFICATION_CODE_INVALIDInvalid or expired code{ attemptsRemaining?: number }
VERIFICATION_TOO_MANY_ATTEMPTSToo many attempts{}

Example

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

@Injectable()
export class MyService {
constructor(private phoneVerificationService: PhoneVerificationService) {}

async verify() {
const dto: VerifyPhoneWithCodeDTO = {
phone: '+1234567890',
code: '123456',
};
const result = await this.phoneVerificationService.verifyPhoneWithCode(dto);
console.log(result.message);
}
}

verifyPhoneWithCodeBySub()

Verify phone using user sub and 6-digit code.

async verifyPhoneWithCodeBySub(dto: VerifyPhoneWithCodeBySubDTO): Promise<VerifyPhoneResponseDTO>

Parameters

  • dto - VerifyPhoneWithCodeBySubDTO - Request DTO
    • sub - string - User identifier (UUID v4)
    • code - string - 6-digit verification code
    • challengeSessionId - number (optional) - Restrict validation to a specific challenge session

Returns

Errors

CodeWhenDetails
NOT_FOUNDUser not found{ userId?: string }
PHONE_REQUIREDNo phone on account{}
VERIFICATION_CODE_EXPIREDCode expired{}
VERIFICATION_CODE_INVALIDInvalid or expired code{ attemptsRemaining?: number }
VERIFICATION_TOO_MANY_ATTEMPTSToo many attempts{}

Example

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

@Injectable()
export class MyService {
constructor(private phoneVerificationService: PhoneVerificationService) {}

async verifyBySub() {
const dto: VerifyPhoneWithCodeBySubDTO = {
sub: 'a21b654c-2746-4168-acee-c175083a65cd',
code: '123456',
};
const result = await this.phoneVerificationService.verifyPhoneWithCodeBySub(dto);
console.log(result.message);
}
}