PHP code example of cuongnx / laravel-flexible-otp

1. Go to this page and download the library: Download cuongnx/laravel-flexible-otp 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/ */

    

cuongnx / laravel-flexible-otp example snippets


'model' => App\\Models\\OtpRecord::class, // or the name you chose

use Cuongnx\\LaravelFlexibleOtp\\Facades\\Otp;

// Generate OTP normal (returns plain token, does not send)
$response = Otp::generate('+84123456789');

// Generate OTP with purpose (returns plain token, does not send)
$response = Otp::generate('+84123456789', purpose: 'verify_phone');

// Generate and automatically send the OTP
$response = Otp::generate(
    '+84123456789',
    purpose: 'verify_phone',
    send: true,
    provider: 'zalo_zns' // or 'speedsms', 'mail'
);

// Validate OTP
$result = Otp::validate('+84123456789', '123456', purpose: 'verify_phone');

if ($result->status) {
    // OTP is valid
}

// Check validity without consuming
$isValid = Otp::isValid('+84123456789', '123456', purpose: 'verify_phone');

return [
    'validity' => 10,                    // minutes
    'resend_cooldown' => 2,              // minutes
    'length' => 6,
    'type' => 'numeric',
    'send_provider' => 'none',           // none | mail | zalo_zns | speedsms
    'connection' => 'mongodb',           // mysql | mongodb
    'model' => App\\Models\\OtpRecord::class,

    'purposes' => [
        'login' => [
            'type' => 'numeric',
            'length' => 4,
            'validity' => 5,
            'resend_cooldown' => 1,
            'send_provider' => 'none',   
        ],
        'verify_phone' => [
            'type' => 'alpha_numeric',
            'length' => 6,
            'validity' => 15,
            'resend_cooldown' => 3,
        ],
        // Add more purposes as needed
    ],
];

$schedule->command('otp:clean')->daily();
bash
php artisan vendor:publish --tag=otp-config

config/otp.php
bash
php artisan otp:make-model
bash
php artisan migrate
bash
php artisan make:listener SendOtpListener --event="CuongNX\LaravelFlexibleOtp\Events\OtpGenerated"
bash
php artisan otp:make-listener
bash
php artisan otp:clean