PHP code example of horlerdipo / simple-otp

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

    

horlerdipo / simple-otp example snippets



return [
    'length' => env('OTP_LENGTH', 6),

    'default_channel' => ChannelType::EMAIL->value,

    'expires_in' => env('OTP_EXPIRATION_TIME', 10),

    'hash' => false,

    'email_template_location' => 'vendor.simple-otp.mails.otp',

    'numbers_only' => true,

    'table_name' => 'otps',

    'messages' => [
        'incorrect_otp' => 'This OTP is incorrect',
        'used_otp' => 'This OTP has already been used',
        'expired_otp' => 'This OTP has expired',
        'valid_otp' => 'This OTP is correct',
    ],
];


use Horlerdipo\SimpleOtp\Facades\SimpleOtp;
SimpleOtp::send(destination: "[email protected]", purpose: "login", queue: "default");

use Horlerdipo\SimpleOtp\Facades\SimpleOtp;
$response = SimpleOtp::verify(destination: "[email protected]", purpose: "login", token: "267799");

use Horlerdipo\SimpleOtp\Facades\SimpleOtp;
$response = SimpleOtp::verify(destination: "[email protected]", purpose: "login", token: "267799", options: ['use' => false]);

use Horlerdipo\SimpleOtp\Facades\SimpleOtp;

SimpleOtp::channel(\Horlerdipo\SimpleOtp\Enums\ChannelType::EMAIL->value)
    ->template('vendor.simple-otp.mails.otp')
    ->length(6)
    ->expiresIn(1)
    ->numbersOnly()
    ->hash(false)
    ->send(destination: "[email protected]", purpose: "testing", queue: "default");

use Horlerdipo\SimpleOtp\Facades\SimpleOtp;
SimpleOtp::channel('email')
    ->send(destination: '[email protected]', purpose: 'password_reset', queue: 'email');

use Horlerdipo\SimpleOtp\Facades\SimpleOtp;
SimpleOtp::channel('blackhole')
    ->send('[email protected]', '2fa');

class AppServiceProvider extends ServiceProvider
{

    public function register(): void
    {
        $this->app->booting(function () {
            \Horlerdipo\SimpleOtp\Facades\SimpleOtp::extend('sms', function () {
                return new SmsChannel(
                    length: config()->get('simple-otp.length'),
                    expiresIn: config()->get('simple-otp.expires_in'),
                    hashToken: config()->get('simple-otp.hash'),
                    template: config()->get('simple-otp.email_template_location'),
                    numbersOnly: config()->get('simple-otp.numbers_only'),
                );
            });
        });
    }
}

use Horlerdipo\SimpleOtp\Facades\SimpleOtp;
SimpleOtp::channel('sms')
    ->send('+23470345480896', '2fa');

namespace App\Channels;

use Horlerdipo\SimpleOtp\Channels\BaseChannel;
use Horlerdipo\SimpleOtp\Contracts\ChannelContract;
use Horlerdipo\SimpleOtp\Contracts\OtpContract;
use Horlerdipo\SimpleOtp\DTOs\VerifyOtpResponse;
use Horlerdipo\SimpleOtp\Exceptions\InvalidOtpLengthException;

class SmsChannel extends BaseChannel implements OtpContract, ChannelContract
{
    public function channelName(): string
    {
        return 'sms';
    }

    /**
     * @throws InvalidOtpLengthException
     */
    public function send(string $destination, string $purpose, array $templateData = [], string $queue = 'default'): void
    {
        $token = $this->generateOtp($this->length, $this->numbersOnly);
        $this->storeOtp(
            destination: $destination, token: $token, purpose: $purpose,
            expiration: $this->expiresIn, hashToken: $this->hashToken
        );

        $this->sendOtpToSms($token);
    }

    public function verify(string $destination, string $purpose, string $token, array $options = []): VerifyOtpResponse
    {
        return $this->verifyOtp(
            destination: $destination,
            token: $token,
            purpose: $purpose,
            use: $options['use'] ?? true
        );
    }

    protected function sendOtpToSms(string $token) {
        dd($token);
    }
}

Route::get('/generate-otp', function (\Illuminate\Http\Request $request, \Horlerdipo\SimpleOtp\SimpleOtpManager $otpManager) {
    $otpManager->channel('email')
        ->template('vendor.simple-otp.mails.otp')
        ->hash(false)
        ->numbersOnly()
        ->length(6)
        ->expiresIn(1)
        ->send("[email protected]", "login");
});

Route::get('/verify-otp', function (\Illuminate\Http\Request $request, \Horlerdipo\SimpleOtp\SimpleOtpManager $otpManager) {
    return dd($otpManager->verify("[email protected]", "login", $request->otp));
});

    protected function schedule(Schedule $schedule): void
    {
        //this will run daily and delete otp that have expired in the last 24 hours
        $schedule->command('simple-otp:prune-expired-otp')->daily();
        
        //if you are like me and you prefer classes instead, this will do the same thing as the above
        $schedule->command(PruneExpiredOtpCommand::class)->daily();
    }
bash

php artisan vendor:publish --tag="simple-otp-migrations"

php artisan migrate

bash

php artisan vendor:publish --tag="simple-otp-config"

bash

php artisan vendor:publish --tag="simple-otp-views"