PHP code example of aliirfaan / laravel-simple-otp

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

    

aliirfaan / laravel-simple-otp example snippets


  aliirfaan\LaravelSimpleOtp\SimpleOtpServiceProvider::class,

 'aliirfaan\LaravelSimpleOtp\SimpleOtpServiceProvider',

    'default_profile' => env('OTP_DEFAULT_PROFILE', 'default'),

    'otp_profiles' => [
        'default' => [
            'otp_type' => env('OTP_TYPE', 'numeric'),
            'otp_length' => (int) env('OTP_LENGTH', 6),
            'otp_timeout_seconds' => (int) env('OTP_TIMEOUT_SECONDS', 180),
            'otp_should_simulate' => env('OTP_SHOULD_SIMULATE', false),
            'otp_simulated_code' => env('OTP_SIMULATED_CODE'),
            'otp_retention_days' => (int) env('OTP_RETENTION_DAYS', 0),
        ],
        'password_reset' => [
            'otp_type' => 'alphanumeric',
            'otp_length' => 8,
            'otp_timeout_seconds' => 600,
        ],
    ],



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use aliirfaan\LaravelSimpleOtp\Models\SimpleOtp; // otp model
use aliirfaan\LaravelSimpleOtp\Services\OtpHelperService; // otp helper service
use aliirfaan\LaravelSimpleOtp\Exceptions\ExpiredException;
use aliirfaan\LaravelSimpleOtp\Exceptions\NotFoundException;
use aliirfaan\LaravelSimpleOtp\Exceptions\NotMatchException;

class TestController extends Controller
{
    protected $otpModel;

    /**
     * Load model in constructor using dependency injection
     */
    public function __construct(SimpleOtp $otpModel)
    {
        $this->otpModel = $otpModel;
    }

    /**
     * Include our service using dependency injection
     */
    public function test_send_otp(Request $request, OtpHelperService $otpHelperService)
    {
        // after an action like login, get yout model from the database
        // for recipient, persist otp code directly
        $modelId = 1;
        $yourExampleModelObj = App\ExampleModel::find($modelId);

        // generate OTP using default profile (or pass profile name, e.g. 'password_reset')
        $otpCode = $otpHelperService->generateOtpCode();

        // model type can be anything but it must be unique if you want to send OTP to multiple model classes
        // it can also be the class name of the object. You get it using new \ReflectionClass($yourExampleModelObj))->getShortName()
        $modelType = 'exampleModel'; 
        $phoneNumber = $yourExampleModelObj->phone;

        // pass 'recipient' key to persist for recipient
        // optionally pass 'profile' for a named profile (expiry comes from profile otp_timeout_seconds)
        $otpData = [
            'actor_id' => $modelId,
            'actor_type' => $modelType,
            'otp_intent' => 'OTP_LOGIN',
            // 'profile' => 'password_reset',
        ];

        $persistResult = $otpHelperService->persistOtpCode($otpCode, $otpData);

        // send otp using your own code
        $message = 'Your OTP is: '. $otpCode;
        
    }

    /**
     * Include our service using dependency injection
     */
    public function test_verify_otp(Request $request, OtpHelperService $otpHelperService)
    {
        // normally you will get this via $request
        $modelId = 1;
        $modelType = 'exampleModel';
        $otpIntent = 'OTP_LOGIN',
        $otpCode = '123456';

        // pass 'recipient' key to validate otp code
        $validateOtpData = [
            'actor_id' => $modelId,
            'actor_type' => $modelType,
            'otp_intent' => $otpIntent,
            'device_id' => null,
            'otp_code' => $otpCode,
        ];

        // verify otp
        try {
            $otpCodeIsValid = $otpHelperService->validateOtpCode($validateOtpData);
        } catch (\aliirfaan\LaravelSimpleOtp\Exceptions\NotFoundException $e) {
            //
        } catch (\aliirfaan\LaravelSimpleOtp\Exceptions\NotMatchException $e) {
            //
        } catch (\aliirfaan\LaravelSimpleOtp\Exceptions\ExpiredException $e) {
            //
        } catch (\Exception $e) {
            //
        }
    }
}
bash
 $ php artisan migrate