signaturetech/laravel-otp
Composer 安装命令:
composer require signaturetech/laravel-otp
包简介
A package to generate and manage otp
README 文档
README
LaravelOtp : generate OTP and Validate OTP
Table of contents
Introduction
LaravelOtp is a Laravel package, designed to generate OTP and Validate OTP using simple steps. This packages will show all OTP history.
Todo
- Generate OTP
- Verify OTP
- Methods
- OTP Lists
- Test Cases
Features
- Generate OTP
- Verify OTP
- Get the User OTP List
- Generate custom lenth, expiry and formate OTP
Installation & Configuration
You can install this package via composer using:
composer require signaturetech/laravel-otp
Next run the command below to setup api-response.config file, you can set your configuration.
php artisan vendor:publish --tag=otp-config
Now add the use SignatureTech\LaravelOtp\Traits\Otpable trait to your model.
use SignatureTech\LaravelOtp\Traits\Otpable;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable, Otpable;
}
Generate OTP
Please use below code to generate otp:
- Get User Details
use App\Models\User;
$user = User::first();
- Create Otp Instance
use SignatureTech\LaravelOtp\Otp;
$otp = Otp::for($user->email)->generate();
Note: You can use email/mobile/phone number to generate otp Just pass the detail using for method.
Note: You can use more method to setting otp all methods described in methods section.
- Attach Otp with user
$userOtp = $user->createOtp($otp);
$otp = $user->otp;
Verify OTP
You can verify otp by using below code:
- Get the use details
use App\Models\User;
$user = User::first();
- Get Otp Instance
use SignatureTech\LaravelOtp\Otp;
$otp = Otp::for($user->email)->getOtp();
- Verify Otp
try {
$user->verifyOtp($otp, $request->get('otp'));
} catch (OtpInvalidException $e) {
return $e->getMessage;
} catch (OtpExpiredException $e) {
return $e->getMessage;
}
Create OTP without model
You can also create otp without model using the following:
- Create OTP
use SignatureTech\LaravelOtp\Otp;
$otp = Otp::for($user->email)->create();
Note: You can use email/mobile/phone number to generate otp Just pass the detail using for method.
Note: You can use more method to setting otp all methods described in methods section.
- Verify Otp
try {
$otp->verifyOtp($request->get('otp'));
} catch (OtpInvalidException $e) {
return $e->getMessage;
} catch (OtpExpiredException $e) {
return $e->getMessage;
}
Methods
- Set length of otp
use SignatureTech\LaravelOtp\Otp;
// Set Length of OTP
$otp = Otp::for($user->email)->setLength(4)->generate();
Note: Default length is 6 digit and you can change the default digit to add the OTP_LENGTH=4 in .env or config/otp.php file
- Set Format (Available Format: alpha | alphanumeric | numeric)
use SignatureTech\LaravelOtp\Otp;
// Set Format (Available Format: alpha | alphanumeric | numeric)
$otp = Otp::for($user->email)->setFormat('numeric')->generate();
Note: Default format is numeric and you can change the default format to add the OTP_FORMAT=4 in .env or config/otp.php file
- Set Expiry (In minutes)
use SignatureTech\LaravelOtp\Otp;
// Set Format (Available Format: alpha | alphanumeric | numeric)
$otp = Otp::for($user->email)->setExpiry(20)->generate();
Note: Default expiry is 10 minutes and you can change this to add the OTP_EXPIRY=20 in .env or config/otp.php file
License
- Written and copyrighted ©2022 by Prem Chand Saini (prem@signaturetech.in)
- ResponseBuilder is open-sourced software licensed under the MIT license
signaturetech/laravel-otp 适用场景与选型建议
signaturetech/laravel-otp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.34k 次下载、GitHub Stars 达 18, 最近一次更新时间为 2024 年 09 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「email」 「mobile」 「laravel」 「otp」 「laravel-otp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 signaturetech/laravel-otp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 signaturetech/laravel-otp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 signaturetech/laravel-otp 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Beyonic PHP Library
The skeleton application for NativePHP for Mobile.
PHP SDK for Mobile Message SMS API
Extensible library for building notifications and sending them via different delivery channels.
Email+ extends Kirby's email capabilities by adding support for multiple email services using the same Kirby email API.
Laravel contact us form package to send email and save to database
统计信息
- 总下载量: 1.34k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 18
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-09-01