Laravel package for OTP validation with built-in features like retry and resend mechanism. Built in max retry and max resend blocking. OTP/Security Code can be send over SMS or Email of your choice with user-defined template.
kiranbhor/laravel-otp-validate is a Laravel package for laravel package for otp validation with built-in features like retry and resend mechanism. built in max retry and max resend blocking. otp/security code can be send over sms or email of your choice with user-defined template..
It currently has 0 GitHub stars and 16 downloads on Packagist (latest version v1.2.0).
Install it with composer require kiranbhor/laravel-otp-validate.
Discover more Laravel packages by kiranbhor
or browse all Laravel packages to compare alternatives.
Last updated
This package is for easy setup for OTP validation process. No hassle, just plug and play. Following the steps mentioned below and you will be able to get a fully working OTP Validation system. You can use this later for authentication or e-commerce production selling, order confirmation.
Require this package with composer:
composer require ferdous/laravel-otp-validate
Once the package is added, the service provider and facade will be auto discovered.
Add the ServiceProvider to the providers array in config/app.php:
Ferdous\OtpValidator\OtpValidatorServiceProvider::class
Add the Facade to the aliases array in config/app.php:
'OtpValidator' => Ferdous\OtpValidator\OtpValidatorServiceProvider::class
Once done, publish the config to your config folder using:
php artisan vendor:publish --provider="Ferdous\OtpValidator\OtpValidatorServiceProvider"
This command will create a config/otp.php file.
From the .env file the email configs are setup. No other changes required.
As the SMS Gateways use different methods and also extra headers and params, you may need to update the sms configs in the otp.php file.
Run the following command to create the otps table.
php artisan migrate
It will create a otps table with the required columns.
Add the following Key-Value pair to the .env file in the Laravel application
# Basic OTP Configs
OTP_SERVICE='enabled'
OTP_TIMEOUT=120
OTP_DIGIT=5
OTP_RESEND_SERVICE='enabled'
OTP_MAX_RETRY=2
OTP_MAX_RESEND=1
# Company and Service
OTP_SERVICE_NAME=
OTP_COMPANY_NAME=
# OTP via Email / SMS
OTP_SEND_BY_EMAIL=1
OTP_SEND_BY_SMS=1
# Email Configurations
OTP_EMAIL_FROM=
OTP_EMAIL_FROM_NAME=
OTP_EMAIL_SUBJECT=
# SMS Configurations
OTP_SMSC_URL='https://sms'
OTP_SMSC_METHOD=
OTP_COUNTRY_CODE=
OTP_SMSC_OVER_JSON=
OTP_SMSC_PARAM_TO_NAME=
OTP_SMSC_PARAM_MSG_NAME=
OTP_SMSC_USER=
OTP_SMSC_PASS=
Definition of the features in config are:
Once the template files are published, open resources/views/vendor/template-otp/
Run the following command to create a controller.
php artisan make:controller OtpController
Below is a sample for calling the OTP Validator in OtpController.
namespace App\Http\Controllers;
use Ferdous\OtpValidator\Object\OtpRequestObject;
use Ferdous\OtpValidator\OtpValidator;
use Ferdous\OtpValidator\Object\OtpValidateRequestObject;
class OtpController extends Controller
{
/**
* @return array
*/
public function requestForOtp()
{
return OtpValidator::requestOtp(
new OtpRequestObject('1432', 'buy-shirt', '01711084714', '[email protected]')
);
}
/**
* @param Request $request
* @return array
*/
public function validateOtp(Request $request)
{
$uniqId = $request->input('uniqueId');
$otp = $request->input('otp');
return OtpValidator::validateOtp(
new OtpValidateRequestObject($uniqId,$otp)
);
}
/**
* @param Request $request
* @return array
*/
public function resendOtp(Request $request)
{
$uniqueId = $request->input('uniqueId');
return OtpValidator::resendOtp($uniqueId);
}
}
Add the following to the routes/web.php file.
Route::get('/test/otp-request', 'OtpController@requestForOtp');
Route::get('/test/otp-validate', 'OtpController@validateOtp');
Route::get('/test/otp-resend', 'OtpController@resendOtp');
The below table describes the error codes generated in the response and their corresponding meanings.
{
"code": 201,
"message": "OTP Sent to the recipient",
"requestId": 1432,
"type": "buy-shirt"
}
| Code | Meanings |--------|------------------------------------------ | 201 | Successfully Generated OTP and shared. | 400 | Bad request. | 501 | Resend Service Disabled. | 503 | Service Unavailable.
| Code | Meanings |--------|------------------------------------------ | 200 | Correct OTP. | 400 | Invalid OTP. | 404 | OTP Expired/Not Found. | 413 | Max Retry Exceeded.
MIT
If you are trying to integrate one of most popular SMS gateway of Bangladesh, muthofun is a popular Bulk SMS Gateway in our country. Here is a sample configuration for the Muthofun SMS Gateway
'smsc' => [
'url' => env('OTP_SMSC_URL'),
'method' => env('OTP_SMSC_METHOD', 'GET'),
'add_code' => env('OTP_COUNTRY_CODE',null),
'json' => env('OTP_SMSC_OVER_JSON',1),
'headers' => [],
'params' => [
'send_to_param_name' => env('OTP_SMSC_PARAM_TO_NAME','number'),
'msg_param_name' => env('OTP_SMSC_PARAM_MSG_NAME','msg'),
'others' => [
'user' => env('OTP_SMSC_USER'),
'password' => env('OTP_SMSC_PASS'),
'unicode' => 1
],
]
];
.env file will be as the following
OTP_SMSC_URL='http://clients.muthofun.com:8901/esmsgw/sendsms.jsp?'
OTP_SMSC_METHOD='GET'
OTP_COUNTRY_CODE='88'
OTP_SMSC_OVER_JSON=0
OTP_SMSC_PARAM_TO_NAME='mobiles'
OTP_SMSC_PARAM_MSG_NAME='sms'
OTP_SMSC_USER='YourUserName'
OTP_SMSC_PASS='YourPassWord'
Example for integrating with the infobip SMS platform, renowned SMS Gateway.
using GET method
'smsc' => [
'url' => env('OTP_SMSC_URL'),
'method' => env('OTP_SMSC_METHOD', 'GET'),
'add_code' => env('OTP_COUNTRY_CODE',null),
'json' => env('OTP_SMSC_OVER_JSON',1),
'headers' => [],
'params' => [
'send_to_param_name' => env('OTP_SMSC_PARAM_TO_NAME','number'),
'msg_param_name' => env('OTP_SMSC_PARAM_MSG_NAME','msg'),
'others' => [
'username' => env('OTP_SMSC_USER'),
'password' => env('OTP_SMSC_PASS'),
'from' => 'InfoSMS',
'flash' => true
],
]
];
.env file will be as the following
OTP_SMSC_URL='https://{baseUrl}/sms/1/text/query?'
OTP_SMSC_METHOD='GET'
OTP_COUNTRY_CODE='88'
OTP_SMSC_OVER_JSON=0
OTP_SMSC_PARAM_TO_NAME='to'
OTP_SMSC_PARAM_MSG_NAME='text'
OTP_SMSC_USER='YourUserName'
OTP_SMSC_PASS='YourPassWord'
Sample for integrating with the msg91 SMS gateway.
using GET method
'smsc' => [
'url' => env('OTP_SMSC_URL'),
'method' => env('OTP_SMSC_METHOD', 'GET'),
'add_code' => env('OTP_COUNTRY_CODE',null),
'json' => env('OTP_SMSC_OVER_JSON',1),
'headers' => [],
'params' => [
'send_to_param_name' => env('OTP_SMSC_PARAM_TO_NAME','number'),
'msg_param_name' => env('OTP_SMSC_PARAM_MSG_NAME','msg'),
'others' => [
'authkey' => 'YourAuthKey',
'sender' => 'YourSenderId',
'route' => '1',
'country' => '88',
],
],
'wrapper' => 'sms',
];
.env file will be as the following
OTP_SMSC_URL='https://control.msg91.com/api/v2/sendsms?'
OTP_SMSC_METHOD='POST'
OTP_COUNTRY_CODE='88'
OTP_SMSC_OVER_JSON=1
OTP_SMSC_PARAM_TO_NAME='to'
OTP_SMSC_PARAM_MSG_NAME='text'
OTP_SMSC_USER='YourUserName'
OTP_SMSC_PASS='YourPassWord'