alhaji-aki / laravel-phone-number-verification
一个用于通过 otp 令牌验证手机号码的包
Requires
- php: ^8.1|^8.2|^8.3
- alhaji-aki/laravel-otp-token: ^1.2
- illuminate/contracts: ^10.0|^11.0
- illuminate/database: ^10.0|^11.0
- illuminate/support: ^10.0|^11.0
Requires (Dev)
- ext-pdo_sqlite: *
- larastan/larastan: ^2.0
- laravel/pint: ^1.13
- mockery/mockery: ^1.6
- orchestra/testbench: ^8.0|^9.0
- phpunit/phpunit: ^10.5
This package is auto-updated.
Last update: 2024-09-08 19:52:36 UTC
README
这是一个简单的包,用于帮助验证用户手机号码。该包使用 alhaji-aki/laravel-otp-token 来生成并发送 otp 令牌给用户
安装
您可以通过运行以下命令使用 composer 安装此包
composer require "alhaji-aki/laravel-phone-number-verification"
安装完成后,包将自动注册自身。运行以下命令以发布迁移、配置和语言文件
php artisan phone-number-verification:install
这将在 App\Http\Controllers\Auth
中发布一个 PhoneNumberVerificationController
注意: 此包不适用于视图,如果您想显示视图而不是 JSON 响应,则需要做一些小的更改。
由于此包使用 alhaji-aki/laravel-otp-token,您需要发布其文件
php artisan vendor:publish --provider="AlhajiAki\OtpToken\OtpTokenServiceProvider"
发布文件后,您可以运行迁移
php artisan migrate
让您的模型实现 CanSendOtpToken
接口,并像这样使用 CanSendOtpToken
特性
<?php namespace App\Models; use AlhajiAki\OtpToken\Contracts\CanSendOtpToken as CanSendOtpTokenContract; use AlhajiAki\OtpToken\Traits\CanSendOtpToken; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; class User extends Authenticatable implements CanSendOtpTokenContract { use Notifiable, CanSendOtpToken; }
要了解更多信息,请查看 alhaji-aki/laravel-otp-token。
现在让您的模型实现 MustVerifyPhoneNumber
接口和 MustVerifyPhoneNumber
特性,以便允许用户验证其手机号码。如下所示
<?php namespace App\Models; use AlhajiAki\OtpToken\Contracts\CanSendOtpToken as CanSendOtpTokenContract; use AlhajiAki\OtpToken\Traits\CanSendOtpToken; use AlhajiAki\PhoneNumberVerification\Contracts\MustVerifyPhoneNumber as MustVerifyPhoneNumberContract; use AlhajiAki\PhoneNumberVerification\Traits\MustVerifyPhoneNumber; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; class User extends Authenticatable implements CanSendOtpTokenContract, MustVerifyPhoneNumberContract { use Notifiable, CanSendOtpToken, MustVerifyPhoneNumber; }
为了正确工作,您需要在您的用户模型中重写三个方法。它们是
phoneNumberAttribute()
:这是表示数据库中手机号码列的属性。phoneNumberVerificationAttribute()
:这是一个时间戳属性,将用于更新以指示用户已验证。sendPhoneNumberVerificationNotification()
:这是发送通知的地方。这里接收要发送的令牌。
一个示例实现是
<?php namespace App\Models; use AlhajiAki\OtpToken\Contracts\CanSendOtpToken as CanSendOtpTokenContract; use AlhajiAki\OtpToken\Traits\CanSendOtpToken; use AlhajiAki\PhoneNumberVerification\Contracts\MustVerifyPhoneNumber as MustVerifyPhoneNumberContract; use AlhajiAki\PhoneNumberVerification\Traits\MustVerifyPhoneNumber; use App\Notifications\Auth\VerifyPhoneNumber; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; class User extends Authenticatable implements CanSendOtpTokenContract, MustVerifyPhoneNumberContract { use Notifiable, CanSendOtpToken, MustVerifyPhoneNumber; public function phoneNumberAttribute(): string { return 'phone_number'; } public function phoneNumberVerificationAttribute(): string { return 'phone_number_verified_at'; } public function sendPhoneNumberVerificationNotification(string $token): void { $this->notify(new VerifyPhoneNumber($token)); } }
如果您想当用户在您的应用程序上注册时通知他们,请将 SendPhoneNumberVerificationNotification
监听器添加到 EventServiceProvider
中的 Registered
事件监听器中。如下所示
/** * The event listener mappings for the application. * * @var array<class-string, array<int, class-string>> */ protected $listen = [ ... Registered::class => [ SendPhoneNumberVerificationNotification::class, ], ... ];
不要忘记导入完整的命名空间 use \AlhajiAki\PhoneNumberVerification\Listeners\SendPhoneNumberVerificationNotification;
最后,您需要在您的 Http Kernel 中注册 \AlhajiAki\PhoneNumberVerification\Middleware\EnsurePhoneNumberIsVerified::class
中间件。示例
protected $routeMiddleware = [ ... 'mobile-verified' => \AlhajiAki\PhoneNumberVerification\Middleware\EnsurePhoneNumberIsVerified::class, ... ];
然后您可以使用该中间件保护您的路由。
测试
composer test
格式化
composer format
静态分析
composer analyse
贡献
请参阅 CONTRIBUTING 以获取详细信息。
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件。