kohaku1907 / lara2step
这是我创建的包 lara2step
v1.0.0
2023-11-07 09:56 UTC
Requires
- php: ^8.1
- illuminate/contracts: ^10.0
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- nunomaduro/collision: ^7.8
- orchestra/testbench: ^8.8
- pestphp/pest: ^2.20
- pestphp/pest-plugin-arch: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
README
Lara2Step 是一个 Laravel 包,为您的 Laravel 应用程序提供两步验证。
安装
使用 composer 安装此包
composer require kohaku1907/lara2step
使用以下命令发布和运行迁移
php artisan vendor:publish --tag="2step-migrations"
php artisan migrate
使用以下命令发布配置文件
php artisan vendor:publish --tag="2step-config"
这是已发布的配置文件的内容
return [ 'default_channel' => 'email', // email, sms 'table_name' => 'two_step_auths', // table name 'code_length' => 4, // code length 'numeric_code' => false, // numeric code only 'confirm_key' => '_2fa', // session key name 'timeout' => 300, // timeout of verifed session in minutes 'max_attempts' => 5, // max attempts 'exceed_countdown_minutes' => 1440, // exceed countdown in minutes 'resend_code_seconds' => 60, // resend code in seconds ];
可选地,您可以使用以下命令发布视图
php artisan vendor:publish --tag="2step-views"
用法
通过以下步骤将 Lara2Step 包集成到您的 Laravel 应用程序中
- 在
User
模型中实现TwoStepAuthenticatable
接口 - 将
TwoStepAuthentication
特性添加到User
模型中
以下是一个 User
模型的示例
use Kohaku1907\Lara2step\Contracts\TwoStepAuthenticatable; use Kohaku1907\Lara2step\TwoStepAuthentication; class User extends Authenticatable implements TwoStepAuthenticatable { use TwoStepAuthentication; public function registerTwoStepAuthentication(): void { $this->configureForceEnable('email'); $this->configureCodeFormat(length: 4, numericCode: true); } }
在 registerTwoStepAuthentication
方法中,您可以配置用户的两步验证设置。以下方法可用
configureForceEnable(string $channel)
:强制为用户启用两步验证。用户将无法禁用两步验证。configureCodeFormat(int $length, bool $numericCode)
:配置用户的代码格式。可以配置代码长度以及代码是否应为数字。
- 将别名中间件添加到需要受两步验证保护的路由中
Route::get('/dashboard', function () { // Only verified users... })->middleware('2step');
默认情况下,如果用户未经验证,中间件将重定向用户到名为 2step.confirm 的路由。Lara2step 包含了 TwoStepController 和默认视图,以方便快速开始。您可以使用 php artisan vendor:publish --tag="2step-views"
命令发布视图,并根据需要自定义它们。
use Kohaku1907\Lara2step\Http\Controllers\TwoStepController; use Illuminate\Support\Facades\Route; Route::get('2fa-confirm', [TwoStepController::class, 'form']) ->name('2step.confirm'); Route::post('2fa-confirm', [TwoStepController::class, 'confirm']); Route::post('2fa-resend', [TwoStepController::class, 'resend']) ->name('2step.resend');
更新日志
请参阅 CHANGELOG 了解最近的变化。
贡献
请参阅 CONTRIBUTING 了解详细信息。
许可
MIT 许可证 (MIT)。请参阅 许可文件 了解更多信息。