triverla/laravel-otp

Laravel package for OTP Generation and Validation

v1.0.0 2023-08-18 22:49 UTC

This package is auto-updated.

Last update: 2024-10-19 02:11:47 UTC


README

This package is for easy setup for OTP validation process. No database required.

Installation

NB: This package only supports 7.4+

Via Composer

composer require triverla/laravel-otp

Add Service Provider & Facade

For Laravel 5.5+

Once the package is added, the service provider and facade will be auto discovered.

For Older versions of Laravel

Add the ServiceProvider to the providers array in config/app.php:

Triverla\LaravelOtp\OtpServiceProvider::class

Add the Facade to the aliases array in config/app.php:

'Otp' => Triverla\LaravelOtp\OtpServiceProvider::class

Publish Config

Once done, publish the config to your config folder using:

php artisan vendor:publish --provider="Triverla\LaravelOtp\OtpServiceProvider"

This command will create a config/otp.php file.

Env Variables

Add the following Key-Value pair to the .env file in the Laravel application

OTP_LENGTH=6
OTP_TIMEOUT=15
OTP_MAILABLE_CLASS=Triverla\LaravelOtp\Notifications\NewOtpMail::class
OTP_SMS_CLASS=Triverla\LaravelOtp\Notifications\NewOtpSms::class
OTP_TRANSPORT_EMAIL=true
OTP_TRANSPORT_SMS=false

Custom Notification Classes

You can use custom classes for your Mail and SMS notifications. You can either add it via .env or in config/otp.php

Usage

Import the facade class:

use Triverla\LaravelOtp\Facades\Otp;

or

otp()

Generate an OTP:

$otp = Otp::generate($unique_secret); // string
$otp = otp()->generate($unique_secret); // string

Verify an OTP:

$otp = Otp::generate($unique_secret); 

$check = otp()->verify($otp, $unique_secret); // bool

Regenerate an OTP: This can be used to resend same or different OTP

$otp = otp()->regenerate($unique_secret, false); // true to change OTP and false to use same OTP

Generate an OTP & Send sms or mail notification to recipient:

use Triverla\LaravelOtp\Helpers\OtpNotificationRequest;

$sendOtp = Otp::notify(new OtpNotificationRequest(otp()->generate($unique_secret), $emailAddress, $mobileNumber)); // bool

// $emailAddress and $mobileNumber are nullable. You can use both or either.

Contributing

Please feel free to fork this package and contribute by submitting a pull request to enhance the functionalities.

Bugs & Issues

If you notice any bug or issues with this package kindly create and issues here ISSUES

Security

If you discover any security related issues, please email yusufbenaiah@gmail.com.

How can I thank you?

Why not star the github repo and share the link for this repository on Twitter or other social platforms.

Don't forget to follow me on twitter!

Thanks! Benaiah Yusuf

License

The MIT License (MIT). Please see License File for more information.