https://github.com/ichtrojan/laravel-otp
OTP generator and validator
https://github.com/ichtrojan/laravel-otp
laravel one otp package password time token
Last synced: 5 months ago
JSON representation
OTP generator and validator
- Host: GitHub
- URL: https://github.com/ichtrojan/laravel-otp
- Owner: ichtrojan
- Created: 2019-10-06T00:45:22.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-08-18T20:12:07.000Z (about 1 year ago)
- Last Synced: 2024-10-24T17:59:56.998Z (12 months ago)
- Topics: laravel, one, otp, package, password, time, token
- Language: PHP
- Homepage:
- Size: 34.2 KB
- Stars: 234
- Watchers: 8
- Forks: 53
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Laravel OTP โฒ
[](https://packagist.org/packages/ichtrojan/laravel-otp) [](https://packagist.org/packages/ichtrojan/laravel-otp) [](https://packagist.org/packages/ichtrojan/laravel-otp)
## Introduction ๐
This is a simple package to generate and validate OTPs (One Time Passwords). This can be implemented mostly in Authentication.
## Installation ๐ฝ
Install via composer
```bash
composer require ichtrojan/laravel-otp
```Run Migrations
```bash
php artisan migrate
```## Usage ๐งจ
>**NOTE**
>Response are returned as objects. You can access its attributes with the arrow operator (`->`)### Generate OTP
```php
generate(string $identifier, string $type, int $length = 4, int $validity = 10);
```* `$identifier`: The identity that will be tied to the OTP.
* `$type`: The type of token to be generated, supported types are `numeric` and `alpha_numeric`
* `$length (optional | default = 4)`: The length of token to be generated.
* `$validity (optional | default = 10)`: The validity period of the OTP in minutes.#### Sample
```php
generate('michael@okoh.co.uk', 'numeric', 6, 15);
```This will generate a six digit OTP that will be valid for 15 minutes and the success response will be:
```object
{
"status": true,
"token": "282581",
"message": "OTP generated"
}
```### Validate OTP
```php
validate(string $identifier, string $token)
```* `$identifier`: The identity that is tied to the OTP.
* `$token`: The token tied to the identity.#### Sample
```php
validate('michael@okoh.co.uk', '282581');
```#### Responses
**On Success**
```object
{
"status": true,
"message": "OTP is valid"
}
```**Does not exist**
```object
{
"status": false,
"message": "OTP does not exist"
}
```**Not Valid***
```object
{
"status": false,
"message": "OTP is not valid"
}
```**Expired**
```object
{
"status": false,
"message": "OTP Expired"
}
```### Check The validity of an OTP
To verify the validity of an OTP without marking it as used, you can use the `isValid` method:
```php
isValid(string $identifier, string $token);
```This will return a boolean value of the validity of the OTP.
### Delete expired tokens
You can delete expired tokens by running the following artisan command:
```bash
php artisan otp:clean
```
You can also add this artisan command to `app/Console/Kernel.php` to automatically clean on scheduled
```php
command('otp:clean')->daily();
}
```## Contribution
If you find an issue with this package or you have any suggestion please help out. I am not perfect.