larakeeps / authguard-otp
AuthGuard OTP is a package intended for random code generation, validation and confirmation (OTP).
Requires
- illuminate/collections: ^v10.10
- illuminate/database: ^v10.10
- illuminate/http: ^10.10
- illuminate/support: ^10.10
Requires (Dev)
- orchestra/testbench: ^8.19
- phpunit/phpunit: ^10.5
README
AuthGuard OTP
AuthGuard OTP is a package intended for random code generation, validation and confirmation (OTP). Normally the OTP is used with a validator code or token that is sent by email or SMS to authenticate or authorize a certain action in the project.
This repository is only compatible with laravel: 7.*
to 11.*
Installation
First Step, execute the command.
composer require larakeeps/authguard-otp
Second step, run the migration to create the tables: auth_guard_otp_codes
php artisan migrate
Third step, publish the authguard configuration, to publish the configurations run the command below:
php artisan vendor:publish --tag=authguard-otp-config
Generating otp code
use Larakeeps\AuthGuard\Facades\OTP; /** * * The create method has 3 parameters, 1 mandatory and 2 optional. * * The $reference parameter is used with the assertive condition function in code validation. * The $email parameter is used in the same way as the $reference parameter * * @param string $phone : required * @param string $email : optional * @param string $reference : optional * * @method static OTP create(string $phone, string|null $email, string|null $reference) * * */ $createOTP = OTP::create('phone number', 'email', 'reference'); // To retrieve the method return, call the following methods below. /** * * The then method returns 2 parameters, $data of type AuthGuard, and $response of type Collection. * * @method then(Closure $destination) * * */ $createOTP->then(function (AuthGuard|null $data, Collection $response){ /* * the $data parameter returns the null or Authguard model containing the table columns. */ /* * The $response parameter returns a collection containing the following data: * number_digits, code, message, status, user.access_token, user.ip_address */ return $data->expires_at; // returns a value of type Carbon::class; if($response->status){ return $response->message; } }); // OR through the get() method that returns a Collection return $createOTP->get(); // OR like this return OTP::get(); // OR through the getResponse() method that returns a Collection return $createOTP->getResponse(); // OR like this return OTP::getResponse(); // OR can be called individually using methods return OTP::getData()->expires_at; // returns a value of type Carbon::class; if(OTP::getStatus()){ return OTP::getMessage(); } return OTP::getAccessToken(); return OTP::getIpAddress();
Checking for the existence of generated code and viewing the generated data.
use Larakeeps\AuthGuard\Facades\OTP; /** * * Method to check if the generated code exists. * $phone parameter is used for better code verification assertiveness. * * @method static bool hasCode(string $code, string|null $phone) * * */ $hasCode = OTP::hasCode('154896'); if($hasCode){ return "The code exist." } /** * * Method for finding and returning data. * $phone parameter is used for better code verification assertiveness. * * @method static OTP getByCode(string $code, string|null $phone) * * */ $authGuardFounded = OTP::getByCode('154896', '5521985642205'); if($authGuardFounded){ return $authGuardFounded }
Confirm whether the code entered is valid and whether it was actually confirmed.
use Larakeeps\AuthGuard\Facades\OTP; /** * * The create method has 3 parameters, 2 mandatory and 1 optional. * * The $reference parameter is used with the assertive condition function in code validation. * * @param string $code : required * @param string $phone : required * @param string $reference : optional * * @method static OTP confirm(string $code, string $phone, string|null $reference) * * */ $validateCode = OTP::confirm('154896', '5521985642205'); // To retrieve the method return, call the following methods below. /** * * The then method returns 2 parameters, $data of type AuthGuard, and $response of type Collection. * * @method then(Closure $destination) * * */ $validateCode->then(function (null $data, Collection $response){ /* * * Within the confirm() method, the $data parameter will always return a null value. * * The $response parameter returns a collection containing the following data: * number_digits, code, message, status, user.access_token, user.ip_address */ if($response->status && OTP::isConfirmed()){ return $response->message; } return $response->message; }); // OR through the getResponse() method that returns a Collection return $createOTP->getResponse(); // OR like this return OTP::getResponse(); // OR can be called individually using methods if(OTP::getStatus()){ return OTP::getMessage(); } return OTP::getAccessToken(); return OTP::getIpAddress();
Deleting an OTP code.
use Larakeeps\AuthGuard\Facades\OTP; /** * * @method OTP deleteCode(string $code) * * */ $deletedCode = OTP::deleteCode(); // To retrieve the method return, call the following methods below. /** * * The then method returns 2 parameters, $data of type AuthGuard, and $response of type Collection. * * @method then(Closure $destination) * * */ $validateCode->then(function (null $data, Collection $response){ /* * * Within the deleteCode() method, the $data parameter will always return a null value. * * The $response parameter returns a collection containing the following data: * number_digits, code, message, status, user.access_token, user.ip_address */ if($response->status){ return $response->message; } }); // OR through the getResponse() method that returns a Collection return $createOTP->getResponse(); // OR like this return OTP::getResponse(); // OR can be called individually using methods if(OTP::getStatus()){ return OTP::getMessage(); } return OTP::getAccessToken(); return OTP::getIpAddress();
Don't forget to follow me on github and star the project.
My contacts
E-mail: douglassantos2127@gmail.com
Linkedin: Acessa Perfil