npm i gen-otp
it is a simple OTP generation and it's exiration project and developers can simply integrate it to their project for OTP verifications and they can customize the OTP expiration time and values of the OTP and also can customize the length of the OTP as their wish :
import generateOTP from 'gen-otp';
//Generates OTP
const genOTP = generateOTP({
length: 4,
digits: true,
letters: true,
symbols: true,
expiration: '3m', //OTP expires in 3 minutes
})
console.log(genOTP)
{ otp: '482113', expiresAt: 2023-09-08T07:32:22.359Z }
let charset = '';
if (digits) charset += '0123456789';
if (letters) charset += 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
if (symbols) charset += '!@#$%^&*()_+-=[]{}|;:,.<>?';
if (!charset) {
throw new Error('At least one character type (digits, letters, symbols) must be selected.');
}
if (length <= 0) {
throw new Error('OTP length must be greater than 0.');
}
value | Type | Default Value | Description |
---|---|---|---|
length | Number | 0 | the length must be greater than or equal to one else throw an error |
digits | Boolean | true | Boolean. default value true and needed to select one of this |
letters | Boolean | false | Boolean. Whether the password must contain at least one number. |
symbols | Boolean | false | Boolean. defaultly fale and if true shows symbols |
expiration | Number | 0 | needed to enter only s for second m for minute and h for hours (Eg:1s,1m,1h) |
otp
: The OTP generated by the function by the customizations of the developer.expiresAt
: If entered the expiration time it will be showing if doesn't entered no expiration for the otp.