PHP code example of turahe / otp

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

    

turahe / otp example snippets


'providers' => [
    // ...
    Turahe\Otp\OtpServiceProvider::class,
],

'aliases' => [
    // ...
    'Otp' => Turahe\Otp\Facades\Otp::class,
],

return [
    // Token expiry time in minutes
    'expires' => 15,
    
    // Database table name
    'table' => 'otp_tokens',
    
    // Password generator type (string, numeric, numeric-no-0)
    'password_generator' => 'numeric',
    
    // Default notification channels
    'default_channels' => 'mail',
];

use Turahe\Otp\Facades\Otp;

// Generate OTP for email (default 15 minutes expiry)
$otp = Otp::generate('[email protected]');

// Generate OTP with custom expiry (10 minutes)
$otp = Otp::generate('[email protected]', 10);

// Generate OTP for phone number
$otp = Otp::generate('+1234567890', 5);

// Validate OTP
$isValid = Otp::validate('[email protected]', '123456');

if ($isValid) {
    // OTP is valid and has been consumed
    echo "OTP verified successfully!";
} else {
    // OTP is invalid or expired
    echo "Invalid or expired OTP";
}

use Turahe\Otp\Jobs\SendOtp;

// Send OTP via email
$otp = Otp::generate('[email protected]');
dispatch(new SendOtp('[email protected]', $otp));

// 1. Generate OTP for login
$otp = Otp::generate($user->email, 10);

// 2. Send OTP via email
dispatch(new SendOtp($user->email, $otp));

// 3. User enters OTP
$userOtp = request('otp');

// 4. Validate OTP
if (Otp::validate($user->email, $userOtp)) {
    // Login successful
    Auth::login($user);
    return redirect()->intended('/dashboard');
} else {
    // Invalid OTP
    return back()->withErrors(['otp' => 'Invalid or expired OTP']);
}

use Turahe\Otp\Helpers;

// Validate Indonesian phone number
$phone = '+6281234567890';
if (validation_number($phone, 'ID')) {
    $formatted = format_number($phone, 'ID');
    // +62 812-3456-7890
}

use Turahe\Otp\Helpers;

$email = '[email protected]';
$provider = get_email_provider($email);
// Returns: 'gmail'

// Check if disposable email
if (validate_email($email)) {
    // Email is not disposable
} else {
    // Email is disposable
}
bash
php artisan vendor:publish --provider="Turahe\Otp\OtpServiceProvider"
bash
php artisan migrate
bash
php artisan vendor:publish --tag=otp-views
bash
# Manual cleanup
php artisan otp:clean

# Scheduled cleanup (add to app/Console/Kernel.php)
protected function schedule(Schedule $schedule)
{
    $schedule->command('otp:clean')->daily();
}