Laravel 登录和注册:让其它设备上的会话失效
Laravel 还提供了一种机制,用于将其它设备上的用户 Session 失效和「注销」,而不会使其当前设备上的 Session 失效。首先,你需要保证 Illuminate\Session\Middleware\AuthenticateSession
中间件在你的 app/Http/Kernel.php
类中的 web
中间件组中,并且没有被注释掉:
'web' => [
// ...
\Illuminate\Session\Middleware\AuthenticateSession::class,
// ...
],
然后, 你就可以使用 Auth
facade 上的 logoutOtherDevices
方法。此方法要求用户提供其当前密码,你的应用程序应通过输入表单接受该密码:
use Illuminate\Support\Facades\Auth;
Auth::logoutOtherDevices($password);
{note} 当调用
logoutOtherDevices
方法后,用户的其它 Session 将完全失效,这意味着他们将「退出」他们之前通过身份认证的所有看守器。