PHP code example of asgarihope / pretty-otp

1. Go to this page and download the library: Download asgarihope/pretty-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/ */

    

asgarihope / pretty-otp example snippets


return [
    'otp_expiry'     => 5,   // OTP expiry time in minutes
    'otp_attempts'   => 5,   // Max OTP attempts
    'otp_length'     => 6,   // OTP length
    'otp_retry_time' => 2,   // Retry time in minutes
];

use Illuminate\Support\Facades\Route;

Route::middleware(['otp:key,segment,3'])->group(function () {
    Route::post('/secure-action', [SecureController::class, 'handle']);
});

use PrettyOtp\Laravel\Events\OtpRequested;

class CustomOtpListener
{
    public function handle(OtpRequested $event)
    {
        $otp = $this->generateOtp($event->mobile, $event->segment);
        $message = "Your OTP for {$event->segment} is: {$otp}";

        // Send the OTP via SMS or another notification channel
        $this->sendNotification($event->mobile, $message);
    }

    private function generateOtp($mobile, $segment)
    {
        // Custom OTP generation logic
    }

    private function sendNotification($mobile, $message)
    {
        // Custom notification logic
    }
}

use PrettyOtp\Laravel\Events\OtpRequested;
use PrettyOtp\Laravel\Services\OtpService;

abstract class SendOtpListener {

    protected $otpService;
    /**
     * @var string
     */
    protected $otp;

    public function __construct(OtpService $otpService) {
        $this->otpService = $otpService;
    }

    public function handle(OtpRequested $event) {
        $this->otp = $this->otpService->generateOtp($event->mobile, $event->segment);

        $message = "Your OTP for {$event->segment} is: {$this->otp}";

        // Replace this with your SMS/notification logic
        $this->sendNotification($event->key, $event->mobile, $message);
    }

    abstract function sendNotification(string $key, string $inputValue, string $message);
}
bash
php artisan vendor:publish --tag=otp-config
bash
php artisan vendor:publish --tag=otp-translations