基于Laravel Passport的手机验证码授权
进入到你的项目目录,然后运行以下命令
composer require qiutuleng/laravel-passport-phone-verification-code-grant
如果你的Laravel版本大于等于5.5
,服务提供者将会自动注册到程序中。
其他版本你需要把\QiuTuleng\PhoneVerificationCodeGrant\PhoneVerificationCodeGrantServiceProvider::class
添加到config/app.php
中的providers
属性中。
'providers' => [
/*
* Package Service Providers...
*/
...
\QiuTuleng\PhoneVerificationCodeGrant\PhoneVerificationCodeGrantServiceProvider::class,
]
你可以在app/Providers/AppServiceProvider.php
的register
函数中添加以下代码注册服务提供者。
$app->register(\QiuTuleng\PhoneVerificationCodeGrant\PhoneVerificationCodeGrantServiceProvider::class);
-
你必须在
User
Model中实现\QiuTuleng\PhoneVerificationCodeGrant\Interfaces\PhoneVerificationCodeGrantUserInterface
接口。<?php 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; }
-
在
User
Model中实现接口定义的findOrNewForPassportVerifyCodeGrant
方法和validateForPassportVerifyCodeGrant
方法。/** * 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_number
和verification_code
字段:为此,请在“config/passport.php”中添加字段,例如:
//... 'phone_verification' => [ 'phone_number_request_key' => 'phone', 'verification_code_request_key' => 'verification_code', ], //...
你可以使用POST
方式访问/oautn/token
接口来获取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);
你可以访问 Laravel/Passport 官方文档来了解更多信息。
你可以在此仓库中创建一个 pull requests 。
期待你的想法或代码。
如果你有任何问题,请在 Issues 中提出,我将会尽力为你解决。