chrysanthos/laravel-otp

Laravel 登录 OTP

2.0.6 2024-07-20 12:23 UTC

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

此包允许您在用户登录时设置 OTP 验证流程。

安装

您可以通过 composer 安装此包

composer require chrysanthos/laravel-otp

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="laravel-otp-config"

以下是已发布配置文件的内容

return [

    /**
     * Whether the package will register the routes and listeners.
     */
    'enabled' => false,

    /**
     * The logo image to be shown above otp input.
     */
    'logo' => null,

    /**
     * The notification to be sent to the logged-in user.
     * Override this with your own implementation so that
     * you can customize the channels, message format etc.
     */
    'notification' => \Chrysanthos\LaravelOtp\Notifications\SendOtpToUserNotification::class,

    /**
     * The paths that should be protected by otp. This must be
     * relative paths with no slashes at the start of the string.
     * Use this option in case you have admin login area with tools
     * like Nova/Backpack/Filament and only nedd otp in the main site
     *
     * Sadly the paths must point to the uri path of the route that the login form is submitted to.
     */
    'paths' => [
        '*',
    ],
];

可选地,您可以使用以下命令发布登录视图

php artisan vendor:publish --tag="laravel-otp-views"

用法

将 RedirectToOtpPage 中间件添加到您希望受 OTP 保护的路由中。

use Chrysanthos\LaravelOtp\Middleware\RedirectToOtpPage;

Route::middleware([
    'auth:sanctum',
    RedirectToOtpPage::class,
])->group(function () {
    Route::get('/dashboard', DashboardController::class);
});

您可以通过更改配置中的 notification 键来自定义通知。通过将通知设置为自定义通知类,您可以完全灵活地控制通知的发送方式(渠道、文本等)。

'notification' => \App\Notifications\YourCustomNotification::class,

测试

composer test

更新日志

请参阅 更新日志 获取最近更改的更多信息。

安全漏洞

请通过电子邮件直接联系我。

鸣谢

许可

MIT 许可证(MIT)。请参阅 许可文件 获取更多信息。