Laravel 注销其他带有 Fortify 的设备

Nim*_*mal 0 php laravel laravel-fortify

我使用 Fortify 身份验证构建了一个 Laravel 应用程序。要求当用户登录时,他们应该从所有其他设备注销。在 Laravel 文档中,提到我可以使用该Auth::logoutOtherDevices($password);方法。但目前尚不清楚如何将其与 Fortify 一起使用。

我尝试在函数内部使用它Fortify::authenticateUsing(function (Request $request) {}),但它不起作用,因为它检查类中方法 User内部的实例。logoutOtherDevices()Illuminate\Auth\SessionGuard

通过在类中进行更多挖掘Laravel\Fortify\Http\Controllers\AuthenticatedSessionController,我发现我可以在 中传递自定义登录管道数组,并添加我自己的处理程序以从那里app/config/fortify.php调用该方法。logoutOtherDevices()

我设法让它以这种方式工作。但我觉得这种方法有些不对劲,我想看看是否有一种明显的方法可以做到这一点(我在这里遗漏了什么吗?)

谢谢。

Nim*_*mal 5

添加我当前的解决方案,希望对某人有所帮助。

如果您查看与强化身份验证相关的类,您将看到身份验证流经多个处理程序类的管道。尽管没有记录,您可以自定义此流程并通过覆盖config/fortify.php配置文件中的以下内容来添加额外的处理程序。

'pipelines' => [
        'login' => [
            Laravel\Fortify\Actions\AttemptToAuthenticate::class,
            Laravel\Fortify\Actions\PrepareAuthenticatedSession::class,
            App\Actions\Fortify\LogoutOtherDevices::class
        ]
    ]
Run Code Online (Sandbox Code Playgroud)

如果配置文件中不存在此块,则需要添加此块。前两个是使默认身份验证过程正常运行所必需的。最后一个:App\Actions\Fortify\LogoutOtherDevices::class是我的自定义处理程序,您可以在其中添加代码以从其他设备注销用户。

创建App\Actions\Fortify\LogoutOtherDevices如下所示的类,它将起作用。

<?php

namespace App\Actions\Fortify;

use Illuminate\Support\Facades\Auth;

class LogoutOtherDevices
{
    /**
     * Handle the incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  callable  $next
     * @return mixed
     */
    public function handle($request, $next)
    {
        // logout other devices
        Auth::logoutOtherDevices($request->password);
        return $next($request);
    }
}
Run Code Online (Sandbox Code Playgroud)

这很好用。但这没有在任何地方记录,因此他们有可能随时改变这种行为。这就是我问这个问题的原因,看看是否还有其他方法可以做到这一点。