albertojm8 / laravel-sms-verification
使用AWS SNS发送和验证短信验证码的库
Requires
- aws/aws-sdk-php: ^3.62
This package is not auto-updated.
Last update: 2024-09-25 11:33:54 UTC
README
使用AWS SNS发送和验证短信验证码的库
此包包含一个特性和自定义验证规则,您可以在任何模型上使用这些规则来通过短信验证码进行验证。
目录
安装
使用composer拉取库
composer require albertojm8/laravel-sms-verification
在config/app.php
中添加服务提供者
Albertojm8\SMSVerification\SMSVerificationServiceProvider::class,
您可以将短信验证添加到任何模型,并且它将创建
- 一个函数 setSMSVerificationNumber($mobile) - 设置手机号码并发送包含验证码的短信消息
- 一个函数 verifySMSCode($code) - 验证短信验证码
- 以及一些用于验证短信验证码的私有方法。您可以在特性本身中查看更多详细信息。
示例
将VerifiesSMSCode
特性添加到您的User模型(或您可能想要启用双因素认证的任何其他模型)
<?php namespace App; use Albertojm8\SMSVerification\Traits\VerifiesSMSCode; use Laravel\Passport\HasApiTokens; use Illuminate\Notifications\Notifiable; use Jenssegers\Mongodb\Auth\User as Authenticatable; class User extends Authenticatable { use HasApiTokens, Notifiable, VerifiesSMSCode; ...
对于MySQL数据库,您需要在模型的迁移中添加这些属性
$table->string('sms_verification_number')->default(''); // you can index this if you want $table->string('sms_verification_code')->default(''); $table->boolean('sms_verification_status')->default(false);
就是这样! :) 现在您可以发送短信验证码并在以后验证它们。
发送短信验证码
通过设置短信验证号码属性发送短信
$user->setSMSVerificationNumber($number)
这将向指定的$number
发送短信。如果消息发送失败,将抛出一个异常。如果短信发送成功,则将在模型上设置sms_verification_number
、sms_verification_code
和sms_verification_status
属性。
在此之后,您可以在任何时间验证代码。
验证短信验证码
对于简单的验证,您可以使用
$user->verifySMSCode($request->get('code'));
这将设置sms_verification_status
属性为true。如果启用了尝试限制,这将增加用户尝试的次数。一旦超过此限制,将抛出TooManySMSVerificationAttempts异常。
设置短信验证尝试次数限制
您可以通过覆盖实现特性的模型的$sms_verification_attempt_limit
来调整用户验证代码的尝试次数
<?php namespace App; use Albertojm8\SMSVerification\Traits\VerifiesSMSCode; use Laravel\Passport\HasApiTokens; use Illuminate\Notifications\Notifiable; use Jenssegers\Mongodb\Auth\User as Authenticatable; class User extends Authenticatable { use HasApiTokens, Notifiable, VerifiesSMSCode; protected $sms_verification_attempt_limit = 10; ...
此值默认为5。
如果您不想通过尝试次数限制用户,将该变量设置为0。
添加用于验证短信验证码的自定义验证
您可以通过添加验证轻松地在任何自定义请求上验证代码
<?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class SomeCustomRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'code' => 'required|sms-code' ]; } }
验证器将尝试使用已登录的用户(Auth::user())验证代码。
使用路由模型绑定添加用于验证短信验证码的自定义验证
或者,您可以通过利用路由模型绑定并在自定义请求模型中创建验证来利用它
<?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class UpdateAccountDetails extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'code' => 'required|sms-code:account' ]; } }
在这种情况下,验证器期望在路由中找到account
对象。该对象需要实现上述提到的特性,以便验证可以正常工作。
更改要发送的短信消息
您可以通过在模型实现中覆盖特性的默认getSMSVerificationMessage($code)
函数来设置通过短信发送的文本
class User extends Model { ... /** * Gets the message to be sent with the SMS * * @param $code * @return string */ public function getSMSVerificationMessage($code) { return "Here is your code for " . env("APP_NAME") . ": " . $code; } ... }
更改短信发送者
您可以通过在模型实现中覆盖特性的默认getSMSVerificationMessage($code)
函数来设置短信发送者
class User extends Model { ... /** * Gets the sender of the verification SMS * * @return string */ public function getSMSVerificationSender() { return "CustomName"; } ... }