helium/laravel-sms-verification

Laravel 短信登录验证

1.2 2020-03-18 04:05 UTC

This package is auto-updated.

Last update: 2024-09-18 14:00:27 UTC


README

用于通过 AWS SNS 发送和验证短信验证码的库

此包包含一个特质和自定义验证规则,您可以在任何模型上使用它们以短信验证码进行验证。

这是一个分支自:https://github.com/freelabois/laravel-sms-verification,该分支又分支自:https://github.com/doge-dev/laravel-sms-verification(原始)

我们已为此更新了 Laravel 6.0,并接受了对原始项目的修改 freelabois/laravel-sms-verification PR。

谢谢!

目录

安装

使用 composer 拉取库

composer require helium/laravel-sms-verification

`config/app.php 中添加服务提供者

Helium\SMSVerification\SMSVerificationServiceProvider::class,

将您的 AWS 凭证添加到 .env 文件中

AWS_SMS_ID=your-aws-access-key-id
AWS_SMS_SECRET=your-aws-secret-access-key
AWS_SMS_REGION=your-aws-region

注意:请参阅 AWS 文档以了解有关 AWS 凭证的最佳实践,并可能考虑创建一个仅具有发送短信消息权限的专用用户

您可以通过添加此库中提供的 VerifiesSMS 特质,将短信验证添加到任何模型中,它将创建

  • 一个函数 setSMSVerificationNumber($mobile) - 设置手机号码并发送包含验证码的短信消息
  • 一个函数 verifySMSCode($code) - 验证短信验证码
  • 以及用于验证短信验证码的一组私有方法。您可以在特质本身中查看它们以获取更多详细信息。

示例

VerifiesSMSCode 特质添加到您的 User 模型(或任何您可能希望启用双因素认证的其他模型)

<?php

namespace App;

use DogeDev\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 DogeDev\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;
    protected $sms_

    ...

此值默认为 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";
    }
    
    ...
}