gen-otp
v1.0.4
Published
OTP generation package with expiration time in-build
Downloads
6
Maintainers
Readme
OTP-generator
Installation
To install the package, run the following command:
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 :
developers can use it like so
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)
output
{ otp: '482113', expiresAt: 2023-09-08T07:32:22.359Z }
if doesn't added any value to true it will make an error
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.');
}
and if also the length is less than 1 make an error
if (length <= 0) {
throw new Error('OTP length must be greater than 0.');
}
Bonus idea of the Package
| 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) |
The generateOTP function returns an object with two properties:
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.