首页 > 解决方案 > 登录后的中间件

问题描述

我希望中间件在登录后立即运行以进行两步验证。

我尝试在 Kernel.php 文件的受保护 $ routeMiddleware 内添加以下行 'login' => \ App \ Http \ Middleware \ TwoFactorVerify :: 类。

标签: laravel

解决方案


您希望中间件在Authenticate检查每个身份验证请求的中间件之后运行。在 Kernel.php 中注册你的中间件,然后在你想要的路由上这样做:

Route::get('example', 'ExampleController@example')->middleware(['auth', 'login']);

如果您希望您的中间件应用于一组路由,请这样做:

Route::middleware(['auth', 'login'])
    ->group(function () {
        Route::get('example', 'ExampleController@example');
        Route::get('example2', 'ExampleController@example2');
    });

您可以two_factor_verified => true在用户验证代码后进行设置。然后,在TwoFactorVerify中间件中,您可以像这样检查:

if($request-session()->get('two_factor_verified'))
{
    return $next($request);
}

return new TwoFactorNotVerifiedException();

推荐阅读