PHP code example of qiutuleng / laravel-passport-phone-verification-code

1. Go to this page and download the library: Download qiutuleng/laravel-passport-phone-verification-code 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/ */

    

qiutuleng / laravel-passport-phone-verification-code example snippets


'providers' => [
    /*
     * Package Service Providers...
     */
     ...
     \QiuTuleng\PhoneVerificationCodeGrant\PhoneVerificationCodeGrantServiceProvider::class,
]

$app->register(\QiuTuleng\PhoneVerificationCodeGrant\PhoneVerificationCodeGrantServiceProvider::class);

   
   
   namespace App;
   
   use Laravel\Passport\HasApiTokens;
   use Illuminate\Notifications\Notifiable;
   use Illuminate\Foundation\Auth\User as Authenticatable;
   use QiuTuleng\PhoneVerificationCodeGrant\Interfaces\PhoneVerificationCodeGrantUserInterface;
   
   class User extends Authenticatable implements PhoneVerificationCodeGrantUserInterface
   {
       use HasApiTokens, Notifiable;
   }
   

   /**
    * Find or create a user by phone number
    *
    * @param $phoneNumber
    * @return \Illuminate\Database\Eloquent\Model|null
    */
   public function findOrCreateForPassportVerifyCodeGrant($phoneNumber)
   {
       // If you need to automatically register the user.
       return static::firstOrCreate(['mobile' => $phoneNumber]);
   
       // If the phone number is not exists in users table, will be fail to authenticate.
       // return static::where('mobile', '=', $phoneNumber)->first();
   }
   
   /**
    * Check the verification code is valid.
    *
    * @param $verificationCode
    * @return boolean
    */
   public function validateForPassportVerifyCodeGrant($verificationCode)
   {
       // Check verification code is valid.
       // return \App\Code::where('mobile', $this->mobile)->where('code', '=', $verificationCode)->where('expired_at', '>', now()->toDatetimeString())->exists();
       return true;
   }
   

   //...
       'phone_verification' => [
           'phone_number_request_key' => 'phone',
           'verification_code_request_key' => 'verification_code',
       ],
   //...
   

$http = new GuzzleHttp\Client;

$response = $http->post('http://your-app.com/oauth/token', [
    'form_params' => [
        'grant_type' => 'phone_verification_code',
        'client_id' => 'client-id',
        'client_secret' => 'client-secret',
        'phone_number' => '+8613416292625',
        'verification_code' => 927068,
        'scope' => '',
    ],
]);

return json_decode((string) $response->getBody(), true);