helium/laravel-sms-verification
Composer 安装命令:
composer require helium/laravel-sms-verification
包简介
Laravel SMS Login Verification
关键字:
README 文档
README
Library for sending out and verifying SMS codes using AWS SNS
The package has a trait and a custom validation rule that you can use on any model for verifying with an SMS code.
This is a fork of: https://github.com/freelabois/laravel-sms-verification which is a fork of: https://github.com/doge-dev/laravel-sms-verification (original)
We have updated this for Laravel 6.0 and accepted freelabois/laravel-sms-verification PRs as modification to the
original project.
Thanks!
Table of contents
- Installation
- Example
- Sending out the SMS code
- Verifying the SMS code
- Setting SMS verification attempt limits
- Adding custom Validation for verifying an SMS code
- Adding custom Validation for verifying an SMS code using route model binding
- Changing the SMS message being sent
- Changing the SMS sender
Installation
Pull the lib with composer:
composer require helium/laravel-sms-verification
Add the service provider in `config/app.php`
Helium\SMSVerification\SMSVerificationServiceProvider::class,
Add your AWS credentials to you .env file:
AWS_SMS_ID=your-aws-access-key-id
AWS_SMS_SECRET=your-aws-secret-access-key
AWS_SMS_REGION=your-aws-region
NOTE: Please refer to AWS documentation for best practices regarding AWS credentials and possibly consider creating a dedicated user with limited privileges only for sending SMS messages
You can add SMS verification to any model by adding the VerifiesSMS trait supplied in this library, and it will create:
- a function setSMSVerificationNumber($mobile) - sets the mobile number and sends out the SMS message containing the verification code
- a function verifySMSCode($code) -verifies the SMS code
- and a bunch of private methods that are used for verifying the SMS code. You can check them out in the Trait itself for more details.
Example
Add the `VerifiesSMSCode` trait to your User model (or any other model on which you might want to enable 2FA):
<?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;
...
For MySQL databases you will need to add these attributes to your Model's migration:
$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);
That's it! :) Now you can send out SMS codes and verify them later.
Sending out the SMS code
Send an SMS by setting the SMS verification number attribute:
$user->setSMSVerificationNumber($number)
This will send an sms message to the given `$number`. If the message fails to send an Expception will pop up. If SMS sending succeeds, the `sms_verification_number`, `sms_verification_codeandsms_verification_status` attributes will be set on the model.
After this, you can verify the code at any time.
Verifying the SMS code
For simple verification you can use:
$user->verifySMSCode($request->get('code'));
This will set the `sms_verification_status` attribute to true. If attempt limiting is enabled, this will increment the attempts that the User had. Once this limit is exceeded a TooManySMSVerificationAttempts Exception will be thrown.
Setting SMS verification attempt limits
You can adjust the number of attempts a User can have at verifying the code by overwriting the `$sms_verification_attempt_limit` on the model implementing the trait:
<?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_
...
This value defaults to 5.
If you don't want to limit your users with attempts, set this variable to 0.
Adding custom Validation for verifying an SMS code
You can easily verify the code on any custom request by adding the validation:
<?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'
];
}
}
The validator will try to validate the code using the logged in user (Auth::user()).
Adding custom Validation for verifying an SMS code using route model binding
Or you can leverage route model binding and create a validation in your custom Request model:
<?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'
];
}
}
In this case the validator expects to find an `account` object in the route. The object needs to implement the above mentioned Trait in order for the validation to work.
Changing the SMS message being sent
You can set the text that is sent via SMS by overriding the traits default getSMSVerificationMessage($code) function in your Model implementation:
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;
}
...
}
Changing the SMS sender
You can set the sender by overriding the traits default getSMSVerificationMessage($code) function in your Model implementation:
class User extends Model
{
...
/**
* Gets the sender of the verification SMS
*
* @return string
*/
public function getSMSVerificationSender()
{
return "CustomName";
}
...
}
helium/laravel-sms-verification 适用场景与选型建议
helium/laravel-sms-verification 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 366 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 03 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「sms」 「aws」 「laravel」 「verification」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 helium/laravel-sms-verification 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 helium/laravel-sms-verification 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 helium/laravel-sms-verification 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This package includes a script and fail2ban configuration that allows you to use fail2ban when utilizing AWS elastic load balancer (ELB) and an apache webserver.
Elastic Driver for Laravel Scout
simple api library.
A replica of the AWS Elastic Beanstalk worker SQS daemon (sqsd) in PHP
SDK for payment gateway PlatbaMobilom.sk for PHP7.0
Extensible library for building notifications and sending them via different delivery channels.
统计信息
- 总下载量: 366
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-03-17