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',

'otp_does_expire' => true

'otp_timeout_seconds' => 180

'otp_digit_length' => 6

'otp_should_encode' => false

'otp_should_simulate' => false

'otp_simulate_fillable_digit' => 1



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use aliirfaan\LaravelSimpleOtp\Models\ModelGotOtp; // 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(ModelGotOtp $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
        $modelId = 1;
        $yourExampleModelObj = App\ExampleModel::find($modelId);

        // generate OTP, it will return an array with otp_code and otp_hash key
        $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;

        $otpData = [
            'model_id' => $modelId,
            'model_type' => $modelType,
            'otp_intent' => 'OTP_LOGIN',
            'otp_code' => $otpCode['otp_hash']
        ];

        /**
         * create otp 
         * use createOtp($otpData, false) to add a row for each otp sent
         * use createOtp($otpData) to update if row exists
         */
        $createOtp = $this->otpModel->createOtp($otpData);

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

    /**
     * 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';

        // get otp
        $otpObj = $this->otpModel->getOtp($modelId, $modelType, $otpIntent);

        // verify otp
        try {
            $otpCodeIsValid = $otpHelperService->otpCodeIsValid($otpObj, $otpCode);
            // update otp validated flag
            $updateOtp = $this->otpModel->updateOtp($otpObj->id);
        } catch (\aliirfaan\LaravelSimpleOtp\Exceptions\NotFoundException $e) {
            //
        } catch (\aliirfaan\LaravelSimpleOtp\Exceptions\NotMatchException $e) {
            //
        } catch (\aliirfaan\LaravelSimpleOtp\Exceptions\ExpiredException $e) {
            //
        } catch (\Exception $e) {
            //
        }
    }
}
bash
 $ php artisan vendor:publish --provider="aliirfaan\LaravelSimpleOtp\SimpleOtpServiceProvider"
bash
 $ php artisan migrate