alexgeno / phone-verification-laravel
一个用于通过 Laravel 通知通道进行手机验证的库。任何通知通道都可以作为发送者,Redis 或 MongoDB 可以作为存储。
v1.0.3
2023-08-09 11:08 UTC
Requires
- php: ^8.0
- alexgeno/phone-verification: ^1.0
Requires (Dev)
- jenssegers/mongodb: ^3.9
- josiasmontag/laravel-redis-mock: ^1.3
- laravel-notification-channels/messagebird: ^4.0
- laravel-notification-channels/twilio: ^3.3
- laravel/pint: ^1.5
- laravel/vonage-notification-channel: ^3.2
- nunomaduro/collision: ^6.2
- orchestra/testbench: ^7.25
- php-mock/php-mock-phpunit: ^2.7
- phpstan/phpstan: ^1.10
- predis/predis: ^2.2
Suggests
- jenssegers/mongodb: required to use MongoDB as a storage
- laravel-notification-channels/messagebird: required to use Messagebird as a sender's notification channel
- laravel-notification-channels/twilio: required to use Twilio as a sender's notification channel
- laravel/vonage-notification-channel: required to use Vonage as a sender's notification channel
- predis/predis: required to use Redis as a storage
This package is auto-updated.
Last update: 2024-08-30 01:44:18 UTC
README
在现代网站或移动应用上注册或登录通常遵循以下步骤
- 用户通过提交手机号码开始验证
- 用户收到带有一次性密码(OTP)的短信或电话
- 用户通过提交 OTP 完成验证
此库基于 alexeygeno/phone-verification-php 构建,并允许设置此功能
支持的功能
- 简单 在不同的存储和通知通道之间切换
- 可配置 OTP 的长度和过期时间
- 可配置速率限制
- 本地化
- 使用不同的 Laravel 方法:自动注入、外观 和 命令
- 记录通知而不是发送真实的通知,对非生产环境有益
- 开箱即用的路由,快速开始
需求
- Laravel 9.x
- 任何可用的 Laravel 通知通道: laravel/vonage-notification-channel、laravel-notification-channels/twilio、laravel-notification-channels/messagebird 和更多
- 任何支持的存储:predis/predis、jenssegers/laravel-mongodb
安装
composer require alexgeno/phone-verification-laravel predis/predis laravel/vonage-notification-channel
注意:配置中的默认存储为 Redis,通知通道为 Vonage
使用
自动注入
public function initiate(\AlexGeno\PhoneVerification\Manager\Initiator $manager) { $manager->initiate('+15417543010'); }
public function complete(\AlexGeno\PhoneVerification\Manager\Completer $manager) { $manager->complete('+15417543010', 1234); }
外观
\AlexGeno\PhoneVerificationLaravel\Facades\PhoneVerification::initiate('+15417543010');
\AlexGeno\PhoneVerificationLaravel\Facades\PhoneVerification::complete('+15417543010', 1234);
命令
php artisan phone-verification:initiate --to=+15417543010
php artisan phone-verification:complete --to=+15417543010 --otp=1234
路由
curl -d "to=+15417543010" localhost/phone-verification/initiate {"ok":true,"message":"Sms has been sent. Check your Phone!"}
curl -d "to=+15417543010&otp=1234" localhost/phone-verification/complete {"ok":true,"message":"The verification is done!"}
注意:默认情况下,包的路由可用。要使它们不可用而不重新定义服务提供程序,请将配置中的布尔键 phone-verification.sender.to_log 更改为 false
配置
[ 'storage' => [ 'driver' => 'redis', // 'redis' || 'mongodb' 'redis' => [ 'connection' => 'default', // the key settings - normally you don't need to change them 'settings' => [ 'prefix' => 'pv:1', 'session_key' => 'session', 'session_counter_key' => 'session_counter', ], ], 'mongodb' => [ 'connection' => 'mongodb', // the collection settings - normally you don't need to change them 'settings' => [ 'collection_session' => 'session', 'collection_session_counter' => 'session_counter', ], ], ], 'sender' => [ 'driver' => 'vonage', // 'vonage' || 'twilio' || 'messagebird' and many more https://github.com/laravel-notification-channels 'channel' => \Illuminate\Notifications\Channels\VonageSmsChannel::class, // \Illuminate\Notifications\Channels\VonageSmsChannel::class || \NotificationChannels\Twilio\TwilioChannel::class || \NotificationChannels\Messagebird\MessagebirdChannel::class and many more https://github.com/laravel-notification-channels 'to_log' => false, // if enabled: instead of sending a real notification, debug it to the app log ], 'routes' => true, // managing the availability of the package routes without redefining the service provider 'manager' => [ 'otp' => [ 'length' => env('PHONE_VERIFICATION_OTP_LENGTH', 4), // 1000..9999 ], 'rate_limits' => [ 'initiate' => [ // for every 'to' no more than 10 initiations over 24 hours 'period_secs' => env('PHONE_VERIFICATION_RATE_LIMIT_INITIATE_PERIOD_SECS', 86400), 'count' => env('PHONE_VERIFICATION_RATE_LIMIT_INITIATE_COUNT', 10), ], 'complete' => [ // for every 'to' no more than 5 failed completions over 5 minutes 'period_secs' => env('PHONE_VERIFICATION_RATE_LIMIT_COMPLETE_PERIOD_SECS', 300), // this is also the expiration period for OTP 'count' => env('PHONE_VERIFICATION_RATE_LIMIT_COMPLETE_COUNT', 5), ], ], ], ];
不同的存储和通知通道
要在不同存储和通知通道之间切换,请安装相应的包并更新配置。例如,要使用 Mongodb 作为存储和 Twilio 作为通知通道
composer require jenssegers/mongodb laravel-notification-channels/twilio
[ 'storage' => [ 'driver' => 'mongodb', // ... ], 'sender' => [ 'driver' => 'twilio', 'channel' => \NotificationChannels\Twilio\TwilioChannel::class, // ... ], // ... ]
如果可用的选项不足,您可以重新定义服务提供程序并添加自定义存储(实现 \AlexGeno\PhoneVerification\Storage\I)或/和发送者(实现 \AlexGeno\PhoneVerification\Sender\I)
发布
配置
php artisan vendor:publish --tag=phone-verification-config
本地化
php artisan vendor:publish --tag=phone-verification-lang
迁移
php artisan vendor:publish --tag=phone-verification-migrations
注意:只有 MongoDB 存储驱动程序需要迁移