npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

one-time-activation-code

v1.0.16

Published

One Time Activation Code

Downloads

40

Readme

One Time Activation Code NPM

Activation Code

NPM package version GitHub issues Coveralls Coverage code style: prettier Twitter

About

If you have authentication in your project, and trying to verify user mobile or email, probably encountered with managing the activation code issue.

With this package, you could solve all your issue in one-time activation codes.

Features

  • Store activation codes in fast cache memory.
  • Delete activation code after a specific time automatically with expiration.
  • Restrict the user to prevent trying more than defined attempts chance.
  • Delete activation code after validation automatically.

Install 🐙

Just add the package into your project.

npm install one-time-activation-code --save

How to use 💡

Then Import it in the target file.

import OneTimeActivationCode from 'one-time-activation-code';

or in old js

const OneTimeActivationCode = require('one-time-activation-code');

Then create an instance.

const otac = new OneTimeActivationCode();

Or you can create the instance with options:

const otac = new OneTimeActivationCode({
  expiresAfter: 500,
  attemptsChance: 3,
  encodeCode: true,
});

| Params | Description | Default | Mandatory | | -------------- | ----------------------------------------------------------------------- | ------------- | --------- | | expiresAfter | Expire and delete activation code after n seconds. | 180 (seconds) | NO | | attemptsChance | Attempts chance to enter wrong validations. It should be more than 0. | 0 | NO | | encodeCode | Store activation code in encoded or cleared string. | true | NO |

Set Activation Code

First of all, you need store the activation code with a unique key.

User Story:

  • Store activation code for user
  • We Send this activation to its email
  • The user's email address is [email protected]
  • And the activation code is 123456
otac.set('[email protected]', '123456');

Validate Activation Code

To validate activation code you should again pass the user entered code with its unique identification key that we store before.

otac.isValid('[email protected]', '123456');

If the activation code was valid it returns true and then delete the activation code from the store.

If the activation code was invalid it returns false and increment the attempts.

Get Activation Code object

The activation code store as an object with two parameters attempts and code.

  • code: The encoded or clear text of activation code that you set before. It stores in encoded Sha256 by default.
  • attempts: Shows how many times the user tries to validate activation code.
otac.get('[email protected]');

Then, it returns:

{
  "code": "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92",
  "attempts": 2
}

If encode was set to false:

{
  "code": "123456",
  "attempts": 2
}

Exceptions

Attempts Restrictions

If you set attemptsChance in options, whenever user attempts reached to attemptsChance then the function throw and exception with ReachedToAttemptsException type. So, you should handle this type of exception.

import { ReachedToAttemptsException } from 'one-time-activation-code';

try {
  otac.isValid('[email protected]', '121212');
} catch (error) {
  if (error instanceof ReachedToAttemptsException) {
    console.log(error.message);
  }
}

//console: Sorry, you've reached to more than 3 attempts. Please try again 1m 22s later.

Not Found Key

As we know if activation coed exceeds to its expiration or maybe activated before, it deleted automatically. So if you try check validation or just get the the activation object, it throw exception with NotFoundKeyException type. So, you should handle this type of exception too.

import { NotFoundKeyException } from 'one-time-activation-code';

try {
  otac.isValid('[email protected]', '121212');
} catch (error) {
  if (error instanceof NotFoundKeyException) {
    console.log(error.message);
  }
}

//console: Sorry, there is no activation code. Please try again to get new code.

So the user should request for new activation code.

Contributing 🍰

Please make sure to read the Contributing Guide before making a pull request.

Thank you to all the people who already contributed to this project!

Maintainers 👷

List of maintainers, replace all href, src attributes by your maintainers datas.

License ⚖️

MIT