1.0.4 • Published 6 months ago

gen-otp v1.0.4

Weekly downloads
-
License
ISC
Repository
github
Last release
6 months ago

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

valueTypeDefault ValueDescription
lengthNumber0the length must be greater than or equal to one else throw an error
digitsBooleantrueBoolean. default value true and needed to select one of this
lettersBooleanfalseBoolean. Whether the password must contain at least one number.
symbolsBooleanfalseBoolean. defaultly fale and if true shows symbols
expirationNumber0needed 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.