PHP code example of nealyip / laravel-otp-validation

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

    

nealyip / laravel-otp-validation example snippets


Nealyip\LaravelOTPValidation\Providers\OTPServiceProvider::class,

// for example
'transport'    => \Nealyip\LaravelOTPValidation\Transport\MailTransport::class,

namespace App\SMS\Transport;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Nealyip\LaravelOTPValidation\Transport\TransportInterface;

class SMSTransport implements TransportInterface {
    /**
     * @var Client 
     */
    private $_client;

    public function __construct()
    {
        $this->_client = new Client();
    }

    /**
     * @inheritDoc
     */
    public function type()
    {
        return static::TYPE_SMS;
    }
    
    /**
     * @inheritdoc
     */
    public function send($phone_number, $message)
    {

        $sms_url = 'https://someurl';
        $parsed_url = parse_url($sms_url);
        $host       = $parsed_url['host'];

        try {

            $this->_client->request('POST', $sms_url, [
                'http_errors' => true,
                'headers'     => [
                    'Host'         => $host,
                    'Authorization'=> 'Bearer ' . config('sms.auth_code') ,
                    'Content-Type' => 'application/json',
                    'Accept'       => 'application/json'
                ],
                'json'        => compact('phone_number', 'message')
            ]);
        } catch (GuzzleException $e) {
            throw new \Exception(trans('error.fail_to_send'), 0, $e);
        }
    }
}

'transport'    => App\SMS\Transport\SMSTransport::class,

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements Nealyip\LaravelOTPValidation\OTP\OTPTarget {

    /**
     * Provide the email for the user used by the Email Provider, 
     * the return value may varies by the scene.
     * May return empty string if you use only SMS otp.
     *
     * @param string $scene
     * @return string
     */
    public function otpEmail($scene = null)
    {

        return $this->email;
    }
    
    /**
     * Provide the mobile phone number for the user used by the SMS Provider,
     * the return value may varies by the scene.
     *
     * @param null $scene
     * @return string
     */
    public function otpMobile($scene = null)
    {

        return $this->country_code . $this->area_code . $this->mobile_number;
    }

    /**
     * A unique user id, for example
     *
     * @return string
     */
    public function otpIdentifier()
    {
        return $this->id;
    }
}

Route::group(['prefix' => 'backend'], function(){
    otp_validation_routes();
});

use Nealyip\LaravelOTPValidation\Services\OTPValidationService;

class FormController{

protected $_service;

public function __construct(OTPValidationService $service) {
    $this->_service = $service;
}

public function create(){
    ...
    $user = request()->user();
    $payload = $this->_service->scene('changepassword')->send($user, ['id' => $user->id], [], 'You are about to change your password, please complete with this one time password :otp');
    
    return response()->json(['sent' => true, 'key' => $payload->key]);   
}

use Nealyip\LaravelOTPValidation\Services\OTPValidationService;

class FormController{

protected $_service;

public function __construct(OTPValidationService $service) {
    $this->_service = $service;
}

public function create(Request $request){
    ...
    
        $user = request()->user();
    if ($request->input('otp')) {
        // WrongTargetException will be thrown if the otp is incorrect
        $this->_service->scene('changepassword')->consume($user, ['id' => $user->id], $request->input('otp'));    
        
        // success validate the otp
        // do your staff here
        
        return .....
    } else {
        $payload = $this->_service->scene('changepassword')->send($user, ['id' => $user->id], [], 'You are about to change your password, please complete with this one time password :otp');
        
        return response()->json(['sent' => true, 'key' => $payload->key]);
    }   
}
bash
php .\artisan vendor:publish --provider=Nealyip\LaravelOTPValidation\Providers\OTPServiceProvider

POST /backend/otp_validation/{key}