sophat / laravel-otp
Laravel 的 OTP 包
Requires (Dev)
- php: ^7.4|^8.0
- hi-folks/rando-php: ^0.2.0
- laravel/framework: ^8|^9|^10|^11
This package is auto-updated.
Last update: 2024-10-03 02:56:08 UTC
README
简介
基于类系统的 Laravel OTP 包。每个 Otp 都是一个执行特定任务的类。例如,一个 EmailVerificationOtp 将标记账户为已验证。
安装
通过 composer 安装
composer require sophat/laravel-otp
发布配置文件
php artisan vendor:publish --provider="Sophat\LaravelOtp\OtpServiceProvider"
使用
生成 OTP
php artisan make:otp {name}
新的 Otp 类将生成到 app/Otp 目录中。例如
php artisan make:otp UserRegistrationOtp
每个 Otp 必须实现 process 方法,该方法将在验证后调用。在那里,Otp 可以执行必要的操作并返回任何结果。
<?php namespace App\Otp; use Sophat\LaravelOtp\Contracts\OtpInterface as Otp; use Illuminate\Auth\Events\Registered; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Hash; use App\Models\User; class UserRegistrationOtp implements Otp { /** * Constructs Otp class */ public function __construct( public string $name, public string $email, public string $password ) { // } /** * Processes the Otp * * @return User */ public function process() { /** @var User */ $user = User::unguarded(function () { return User::create([ 'name' => $this->name, 'email' => $this->email, 'password' => Hash::make($this->password), 'email_verified_at' => now(), ]); }); event(new Registered($user)); Auth::login($user); return $user; } }
发送 OTP
<?php use Sophat\LaravelOtp\Facades\Otp; Otp::identifier($identifier)->send($otp, $notifiable);
$otp:要发送的 otp。$notifiable:匿名可通知或可通知实例。
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Notification; use Illuminate\Http\Request; use Illuminate\Validation\Rules; use Sophat\LaravelOtp\Facades\Otp; use App\Models\User; use App\Otp\UserRegistrationOtp; Route::post('/register', function(Request $request){ $request->validate([ 'name' => ['required', 'string', 'max:255'], 'email' => ['required', 'string', 'email', 'max:255', 'unique:' . User::class], 'password' => ['required', Rules\Password::defaults()], ]); $otp = Otp::identifier($request->email)->send( new UserRegistrationOtp( name: $request->name, email: $request->email, password: $request->password ), Notification::route('mail', $request->email) ); return __($otp['status']); });
返回
['status' => Otp::OTP_SENT] // Success: otp.sent
验证 OTP
<?php use Sophat\LaravelOtp\Facades\Otp; Otp::identifier($identifier)->attempt($code);
$code:要比较的 otp 代码。
返回
['status' => Otp::OTP_EMPTY] // Error: otp.empty ['status' => Otp::OTP_MISMATCHED] // Error: otp.mismatched ['status' => Otp::OTP_PROCESSED, 'result'=>[]] // Success: otp.processed
result 键包含 Otp 类的 process 方法的返回值。
<?php use Illuminate\Support\Facades\Route; use Illuminate\Http\Request; use Sophat\LaravelOtp\Facades\Otp; Route::post('/otp/verify', function (Request $request) { $request->validate([ 'email' => ['required', 'string', 'email', 'max:255'], 'code' => ['required', 'string'] ]); $otp = Otp::identifier($request->email)->attempt($request->code); if($otp['status'] != Otp::OTP_PROCESSED) { abort(403, __($otp['status'])); } return $otp['result']; });
不清除缓存验证 OTP
<?php use Sophat\LaravelOtp\Facades\Otp; Otp::identifier($identifier)->check($code);
$code:要比较的 otp 代码。
返回
['status' => Otp::OTP_EMPTY] // Error: otp.empty ['status' => Otp::OTP_MISMATCHED] // Error: otp.mismatched ['status' => Otp::OTP_MATCHED] // Success: otp.matched
重新发送 OTP
<?php use Sophat\LaravelOtp\Facades\Otp; Otp::identifier($identifier)->update();
返回
['status' => Otp::OTP_EMPTY] // Error: otp.empty ['status' => Otp::OTP_SENT] // Success: otp.sent
<?php use Illuminate\Support\Facades\Route; use Illuminate\Http\Request; use Sophat\LaravelOtp\Facades\Otp; Route::post('/otp/resend', function (Request $request) { $request->validate([ 'email' => ['required', 'string', 'email', 'max:255'] ]); $otp = Otp::identifier($request->email)->update(); if($otp['status'] != Otp::OTP_SENT) { abort(403, __($otp['status'])); } return __($otp['status']); });
设置标识符
OTP 类的每个方法都需要设置一个标识符以唯一标识 Otp。
<?php use Sophat\LaravelOtp\Facades\Otp; Otp::identifier($request->email)->send(...);
<?php use Sophat\LaravelOtp\Facades\Otp; Otp::identifier($identifier)->send(...); Otp::identifier($identifier)->attempt(...); Otp::identifier($identifier)->update(); Otp::identifier($identifier)->check(...);
配置
在发布包后,配置文件位于 config/otp.php。
format- 生成 OTP 代码的格式(numeric|alphanumeric|alpha)length- 生成 OTP 代码的长度expires- OTP 过期前的分钟数notification- 要使用的自定义通知类,默认为Sophat\LaravelOtp\OtpNotification
翻译
此包不提供开箱即用的翻译,但这里有一个示例。创建一个新的翻译文件: lang/en/otp.php
<?php return [ /* |-------------------------------------------------------------------------- | OTP Language Lines |-------------------------------------------------------------------------- | | The following language lines are used by the OTP broker | */ 'sent' => 'We have sent your OTP code!', 'empty' => 'No OTP!', 'matched' => 'OTP code verified!', 'mismatched' => 'Mismatched OTP code!', 'processed' => 'OTP was successfully processed!' ];
然后翻译状态
return __($otp['status'])
API
-
Otp::identifier(mixed $identifier)- 设置 OTP 标识符 -
Otp::send(OtpInterface $otp, mixed $notifiable)- 向可通知发送 OTP -
Otp::attempt(string $code)- 尝试 OTP 代码,返回调用 OTP 的process方法的结果 -
Otp::check(string $code)- 将代码与当前 OTP 进行比较,这不会处理或清除 OTP -
Otp::update()- 重新发送并更新当前 OTP -
Otp::clear()- 删除 OTP -
Otp::useGenerator(callable $callback)- 设置要使用的自定义生成器,生成器将使用$format和$length调用 -
Otp::generateOtpCode($format, $length)- 生成 OTP 代码
贡献
欢迎贡献。