andri-andreal/passport-login-phone-number

Resource Login with Phone Number for Laravel Passport

dev-master 2019-09-10 14:29 UTC

This package is not auto-updated.

Last update: 2024-04-18 23:55:34 UTC


README

Installation

composer require andri-andreal/passport-login-phone-number

Register to Laravel/Lumen

Laravel

in config/app.php

\Andreal\PhoneNumberCodeGrant\PhoneNumberCodeGrantServiceProvider::class

'providers' => [
    /*
     * Package Service Providers...
     */
     ...
     \Andreal\PhoneNumberCodeGrant\PhoneNumberCodeGrantServiceProvider::class,
]

Lumen

add in bootstrap/app.php

$app->register(\Andreal\PhoneNumberCodeGrant\PhoneNumberCodeGrantServiceProvider::class);

How To Use?

Add Interface

  1. in User Model add Andreal\PhoneNumberCodeGrant\Interfaces\PhoneNumberCodeGrantUserInterface
<?php

namespace App;

use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Andreal\PhoneNumberCodeGrant\Interfaces\PhoneNumberCodeGrantUserInterface;

class User extends Authenticatable implement PhoneVerificationCodeGrantUserInterface
{
    use HasApiTokens, Notifiable;
}
  1. in User ModelfindOrNewForPassportVerifyCodeGrant add validateForPassportVerifyCodeGrant function
/**
 * 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;
}

Get Token

Use POSTtp Endpoint /oautn/token

$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);