PHP code example of salehhashemi / laravel-otp-manager

1. Go to this page and download the library: Download salehhashemi/laravel-otp-manager library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

salehhashemi / laravel-otp-manager example snippets


use Salehhashemi\OtpManager\Facade\OtpManager;

$sentOtp = OtpManager::send("1234567890");

$sentOtp = OtpManager::sendAndRetryCheck("1234567890");

$isVerified = OtpManager::verify("1234567890", 123456, "uuid-string");

$isDeleted = OtpManager::deleteVerifyCode("1234567890");

protected $listen = [
    \Salehhashemi\OtpManager\Events\OtpPrepared::class => [
        \App\Listeners\SendOtpNotification::class,
    ],
];

use Salehhashemi\OtpManager\Events\OtpPrepared;

class SendOtpNotification
{
    public function handle(OtpPrepared $event)
    {
        $mobile = $event->mobile;
        $otpCode = $event->code;

        // Send the OTP code to the mobile number
        // You can use your preferred SMS service here.
    }
}

use Salehhashemi\OtpManager\Contracts\OtpTypeInterface;

enum MyOtpEnum: string implements OtpTypeInterface
{
    case SIGNUP = 'signup';
    case RESET_PASSWORD = 'reset_password';

    public function identifier(): string
    {
        return $this->value;
    }
}

OtpManager::send('1234567890', MyOtpEnum::SIGNUP);
OtpManager::verify('1234567890', $otpCode, $trackingCode, MyOtpEnum::SIGNUP);

Route::middleware('otp-rate-limiter')->group(function () {
    // Routes that 

    use Salehhashemi\OtpManager\Contracts\MobileValidatorInterface;
    
    class CustomMobileValidator implements MobileValidatorInterface
    {
        public function validate(string $mobile): void
        {
            // Your validation logic here
        }
    }
    

    'mobile_validation_class' => CustomMobileValidator::class,
    
bash
php artisan make:listener SendOtpNotification
bash
php artisan vendor:publish --provider="Salehhashemi\OtpManager\OtpManagerServiceProvider" --tag="config"
bash
php artisan vendor:publish --provider="Salehhashemi\OtpManager\OtpManagerServiceProvider" --tag="lang"
bash
php artisan config:clear
bash
docker-compose exec php bash