@studiosonrai/nestjs-otp
v1.4.0
Published
OTP (One-Time Password) module for NestJS applications with pluggable storage
Readme
@studiosonrai/nestjs-otp
OTP module for NestJS with pluggable storage (in-memory or SQL Server).
Installation
npm install @studiosonrai/nestjs-otp
# For SQL Server storage:
npm install typeormUsage
import { OtpModule, OtpService } from '@studiosonrai/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 |
