npm.io
1.5.0 • Published 1 week ago

@gskglobal/nestjs-otp

Licence
UNLICENSED
Version
1.5.0
Deps
1
Size
253 kB
Vulns
0
Weekly
0

@gskglobal/nestjs-otp

OTP module for NestJS with pluggable storage (in-memory or SQL Server).

Installation

npm install @gskglobal/nestjs-otp

# For SQL Server storage:
npm install typeorm

Usage

import { OtpModule, OtpService } from '@gskglobal/nestjs-otp';

@Module({
  imports: [
    OtpModule.forRootAsync({
      inject: [DataSource],
      useFactory: (dataSource: DataSource) => ({
        storageType: 'sqlserver', // or 'memory'
        sqlServerConfig: { dataSource },
        expiryMinutes: 10,
        onOtpGenerated: async (email, otp) => {
          await sendEmail(email, `Your OTP: ${otp}`);
        },
      }),
    }),
  ],
})
export class AppModule {}

// Inject and use
@Injectable()
export class AuthService {
  constructor(private otpService: OtpService) {}

  async sendOtp(email: string) {
    const { guid } = await this.otpService.generateOtp(email);
    return { guid };
  }

  async verifyOtp(guid: string, otp: string) {
    const result = await this.otpService.verifyOtp(guid, otp);
    if (!result.valid) throw new Error(result.reason);
    return result.identifier;
  }
}

Configuration

Option Type Description
storageType 'memory' | 'sqlserver' Storage backend
sqlServerConfig { dataSource, tableName?, schema? } SQL Server config
expiryMinutes number OTP validity (default: 5)
otpLength number OTP digits (default: 6)
autoCleanup boolean Auto-delete expired OTPs
onOtpGenerated (id, otp, guid, expires) => Promise<void> Callback to send OTP
demoAccounts { identifiers, fixedOtp, expiresAt? } Fixed OTPs for app store reviewers