prasanth-j / otpify
Otpify 是一个 Laravel 扩展包,它提供了一种简单优雅的方式来生成和验证一次性密码。
v1.0.3
2022-08-08 12:26 UTC
Requires
- php: ^8.0
- illuminate/console: ^9.0
- illuminate/database: ^9.0
- illuminate/support: ^9.0
- nesbot/carbon: ^2.35
Requires (Dev)
- illuminate/config: ^9.0
README
简介
Otpify 是一个 Laravel 扩展包,它提供了一种简单优雅的方式来生成和验证一次性密码。
安装
您可以通过 composer 安装此包
composer require prasanth-j/otpify
您可以使用以下命令运行迁移
php artisan migrate
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="otpify-config"
这是发布配置文件的内容
return [ /** * The length of token. */ 'digits' => 6, /** * The expiry time of token in minutes. */ 'validity' => 15 ];
可选地,您可以使用以下命令发布迁移
php artisan vendor:publish --tag="otpify-migrations"
使用方法
1. 生成 OTP
use PrasanthJ\Otpify\Facades\Otpify; Otpify::generate(string $identifier, int $userId, string $otpType, int $digits, int $validity);
$identifier
:与 OTP 相关联的身份(电子邮件或手机号)。$userId (可选 | 默认 = null)
:用户表中的用户 id。$otpType (可选 | 默认 = null)
:OTP 的类别,如登录、验证等。$digits (可选 | 默认 = 6)
:要生成的数字数量,应为 4 到 8。$validity (可选 | 默认 = 15)
:OTP 的有效期限(分钟)。
示例
use PrasanthJ\Otpify\Facades\Otpify; $otp = Otpify::generate('john@example.com', 2, 'verification', 6, 10);
这将生成一个有效期为 10 分钟的六位 OTP,成功响应将如下
{
"status": "success",
"token": "535923",
"message": "OTP genetated successfully"
}
2. 验证 OTP
use PrasanthJ\Otpify\Facades\Otpify; Otpify::validate(string $identifier, string $token, string $otpType);
$identifier
:与 OTP 相关联的身份(电子邮件或手机号)。$token
:与身份关联的令牌。$otpType (可选 | 默认 = null)
:OTP 的类别,如登录、验证等。
示例
use PrasanthJ\Otpify\Facades\Otpify; $otp = Otpify::generate('john@example.com', '535923', 'verification');
响应
成功
{
"status": "success",
"message": "OTP is valid"
}
不存在
{
"status": "error",
"message": "OTP does not exist"
}
错误
{
"status": "warning",
"message": "OTP invalid"
}
过期
{
"status": "error",
"message": "OTP Expired"
}
已验证
{
"status": "info",
"message": "OTP already verified"
}
删除已验证令牌
您可以通过运行以下 artisan 命令来删除已验证令牌
php artisan otpify:clean
您还可以将此 artisan 命令添加到 app/Console/Kernel.php
中,以自动在计划的时间进行清理
protected function schedule(Schedule $schedule) { $schedule->command('otpify:clean')->daily(); }
贡献
如果您在此包中发现问题或有任何建议,请提供帮助。
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅许可证文件。