andri-andreal / passport-login-phone-number
Resource Login with Phone Number for Laravel Passport
Installs: 1 626
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=5.6.4
- laravel/passport: *
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
- in
User
Model addAndreal\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; }
- in
User
ModelfindOrNewForPassportVerifyCodeGrant
addvalidateForPassportVerifyCodeGrant
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 POST
tp 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);