PHP code example of dukstra / otp

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

    

dukstra / otp example snippets


use Dukstra\Otp\Otp;
.
.
$otp =  Otp::generate($identifier);
.
$verify = Otp::validateUsingIdentifier($identifier, $otp->token);
// example response
{
  "status": true
  "message": "OTP is valid"
}

// to get an expiredAt time
$expires = Otp::expiredAt($identifier);

// example response 
{
+"status": true
+"expired_at": Illuminate\Support\Carbon @1611895244^ {
  ....
  #dumpLocale: null
  date: 2021-01-29 04:40:44.0 UTC (+00:00)
}


use Dukstra\Otp\Otp;
.
.
$otp =  Otp::setValidity(30)  // otp validity time in mins
      ->setLength(4)  // Length of the generated otp
      ->setMaximumOtpsAllowed(10) // Number of times allowed to regenerate otps
      ->setOnlyDigits(false)  // generated otp contains mixed characters ex:ad2312
      ->setUseSameToken(true) // if you re-generate OTP, you will get same token
      ->generate($identifier);
.
$verify = Otp::setAllowedAttempts(10) // number of times they can allow to attempt with wrong token
    ->validateUsingIdentifier($identifier, $otp->token);

bash
php artisan vendor:publish --provider="Dukstra\Otp\Providers\OtpServiceProvider" --tag="migrations"
php artisan migrate
bash
php artisan vendor:publish --provider="Dukstra\Otp\Providers\OtpServiceProvider" --tag="config"