One Time Activation Code
About
If you have authentication in your project, and trying to verify user mobile
oremail
, 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.
;
or in old js
const OneTimeActivationCode = ;
Then create an instance.
;
Or you can create the instance with options:
;
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
text@gmail.com
- And the activation code is
123456
otac.set'text@gmail.com', '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'text@gmail.com', '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'text@gmail.com';
Then, it returns:
If encode
was set to false:
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.
; try catch error //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.
; try catch error //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.
Sadra Isapanah Amlashi 💻 |
License ⚖️
MIT