LARAVEL OTP GENERATOR
A laravel package to generate OTP.
🚀 Installation
Composer
will allows you to quickly install via the command line.
composer require eagleeye/otp
🚀 Vendor Publishing
NOTE
Publishing vendors are optional only required if you are willing to change configuration and use database as OTP storage.
Publish config file
php artisan vendor:publish --provider="Eagleeye\Otp\OtpServiceProvider" --tag=config
Publish database migration file
php artisan vendor:publish --provider="Eagleeye\Otp\OtpServiceProvider" --tag=migrations
✨ Configuration File
NOTE
You will find the OTP configuration file inconfig
folder name asotp.php
.
otp.php
<?php
return
[ /**
* [Description for prefix]
*
* Example: G | Output G-12345678
*
*/
'prefix'=>null,
/**
* [Description for boot]
*
* Example: numeric | Output 12345678
* Example: alphabetic | Output ktylnfdgf
* Example: alphanumeric | Output kt7l7fdg9
* Example: mixnumeric | Output !45<45!)
* Example: mixalphabetic | Output !ta<hg!)
* Example: mixalphanumeric | Output !t4<7g!)
*/
'type'=>'numeric',
/**
* [Description for length]
*
* Example: 6 | Output 123456
* Example: 8 | Output 12345678
*
*/
'length'=>'6',
/**
* [Description for Mood]
*
* @return [type]
*
*/
'mood'=>env('APP_ENV'),
/**
* [Description for storage]
*
* Example: databse | OTP will store in databse
* Example: cache | OTP will store in cache
* Example: session | OTP will store in session
*/
'storage'=>'database',
/**
* [Description for expire]
*
* OTP expire time
* In seconds
*
*/
'expire'=>"60",
/**
* [Description for case]
*
* Example: lower | Output werfghyt
* Example: upper | Output AFKUTDFG
*/
'case'=>'lower',
/**
* [Description for table_name]
*
* Table name to create table in databse
*
*/
'table_name'=>'otp_table'
];
-
Prefix
-
The
prefix
default value isnull
. If you would like to add a text or trademark or a single character likegoogle
registrationOTP
in every generated otp, Just add the prefix value.<br>Prefix
and ActualOTP
will be seperated by a (-
)Prefix Without Prefix With Prefix P 12345678 P-12345678
-
-
type
-
The
type
representOTP
string type .default value isnumeric
.<br>Prefix
and ActualOTP
will be seperated by a-
Type Output Character Types numeric 12345678 Number alphabetic ktylnfdgf Alphabet alphanumeric kt7l7fdg9 Number,Alphabet mixnumeric !45<45! Number,Special Character mixalphabetic !tad%hgr Alphabet,Special Character mixalphanumeric !t4<7g! Number,Alphabet,Special Character
-
-
Length
-
The
length
represent total number of character's in generatedOTP
.Default length is 6.Length OTP 6 123456 8 12345678
-
-
Storage
Cache Storage
used as default storage to store OTP .Dont forget to publish Otp migration file before usingdatabase
as OTP storage.storage Options:
cache
session
database
-
Expire
expire
- The validity period of the OTP in seconds
-
Case
-
The
case
- differentiating between capital and lower-case letters.Case OTP lower generator upper GENERATOR
-
-
Table Name
table_name
- With the name OTP databse migration table will be created .otp_table
is default table name.
🚀 Usage
Import OTP facade
use Eagleeye\Otp\Facades\OTP;
get
Static Function ://Facade accessor public function
public function get(String $key)
{
//processing...
}
This will generate a OTP that valid till expire time(configured in config file
),For every otp request the method response new otp string with new expire time:
$key
: The key that will be tied to the OTP.
Example
<?php
$otp = OTP::get('usertoken');
echo $otp;
// 20220317221648
// https://example/url
"267958"
interval
Static Function ://Facade accessor public function
public function interval(String $key)
{
//processing...
}
This will generate a OTP that valid till expire time(configured in config file
),for new request it will generate new otp if only previous otp with the $key
expired or empty:
$key
: The key that will be tied to the OTP.
Example if otp hasnt expired
<?php
$otp = OTP::interval('usertoken');
echo $otp;
// 20220317221648
// https://example/url
array:2 [â–¼
"expired" => false
"remaining" => "00 00:01:49" //Remaining time of expiration
]
Example if otp expired
<?php
$otp = OTP::interval('usertoken');
echo $otp;
// 20220317221648
// https://example/url
array:2 [â–¼
"expired" => true
"otp" => "282561" //New otp
]
action
Static Function ://Facade accessor public function
public function action(String $key,Callable $callback)
{
//processing...
}
This will generate a OTP that valid till expire time(configured in config file
),For every otp request the method response new otp string with new expire time. The function takes one extra callable parameter
to do additional work's(SMS,EMAIL,etc..
) before returning otp string:
$key
: The key that will be tied to the OTP.$callback
: Take a callable function . Specially usefull to implement sms gateway's ,Email,etc...
Example
<?php
$otp = OTP::action('usertoken',function($otp){
//Sms::send('+8801*******',$otp);
});
echo $otp;
// 20220317221648
// https://example/url
"267958"
intervalaction
Static Function ://Facade accessor public function
public function intervalaction(String $key,Callable $callback)
{
//processing...
}
Same as Interval the function will generate new otp until previous one expired:
$key
: The key that will be tied to the OTP.$callback
: Take a callable function . Specially usefull to implement sms gateway's ,Email,etc...
Example if otp hasnt expired
<?php
$otp = OTP::intervalaction('usertoken',function($otp){
//Sms::send('+8801*******',$otp);
});
echo $otp;
// 20220317221648
// https://example/url
array:2 [â–¼
"expired" => false
"remaining" => "00 00:01:49" //Remaining time of expiration
]
Example if otp expired
<?php
$otp = OTP::intervalaction('usertoken',function($otp){
//Sms::send('+8801*******',$otp);
});
echo $otp;
// 20220317221648
// https://example/url
array:2 [â–¼
"expired" => true
"otp" => "282561" //New otp
]
Readonly
Static Function ://Facade accessor public function
public function readonly($options=nullable)
{
//processing...
}
This will return a random generated string for other uses.
$options
: It takes a array as parameter to replace Configuration file parameters.
Example
<?php
$result=OTP::readonly(['prefix'=>'sn','length'=>15,'case'=>'upper','type'=>'alphabetic']);
echo $result;
// 20220331221403
// http://otp.test/
"sn-ISUQXTFPQYJIMSR"
Author
- Github: @shuvodewan
Contribution
If you find an issue with this package or you have any suggestion please help out.