one-time-activation-code
v1.0.16
Published
One Time Activation Code
Downloads
13
Maintainers
Readme
One Time Activation Code
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 inencoded
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