qiutuleng / laravel-passport-phone-verification-code
此包已被弃用且不再维护。作者建议使用qiutuleng/laravel-passport-phone-verification-code-grant 包。
Laravel Passport 的资源所有者手机验证码凭据授权
v2.0.3
2020-02-27 23:03 UTC
Requires
- php: >=5.6.4
- laravel/passport: *
README
介绍
Laravel Passport 的资源所有者手机验证码凭据授权
安装
在您的工作目录下,并在终端运行以下命令
composer require qiutuleng/laravel-passport-phone-verification-code-grant
配置
Laravel
如果您的 Laravel 版本大于或等于 5.5
,服务提供程序将自动附加。
其他版本,您必须在 config/app.php
中的 providers
数组中添加 \QiuTuleng\PhoneVerificationCodeGrant\PhoneVerificationCodeGrantServiceProvider::class
。
'providers' => [ /* * Package Service Providers... */ ... \QiuTuleng\PhoneVerificationCodeGrant\PhoneVerificationCodeGrantServiceProvider::class, ]
Lumen
$app->register(\QiuTuleng\PhoneVerificationCodeGrant\PhoneVerificationCodeGrantServiceProvider::class);
如何使用?
配置
-
您必须在您的
User
模型中实现\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
模型添加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', ], //...
请求令牌
您可以通过向 /oauth/token
路径发出包含用户手机号码和验证码的 POST
请求来请求访问令牌。
$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 中提出问题,我将尽力帮助您。