You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 8 中如何防止同一账号凭证多点同时登录?

Laravel 同账号多端登录限制配置问题

问题描述

  • 开发了支持多用户登录的Laravel Web应用,需要实现账号登录规则:同一账号如果已经在其他设备处于登录状态,使用相同凭证二次登录时要自动注销其他设备的原有登录会话,禁止同账号多端同时在线。
  • 已知Laravel自带logoutOtherDevices方法可实现上述需求,且已经在app/Http/Kernel.php文件中取消了\Illuminate\Session\Middleware\AuthenticateSession::class中间件的注释。
  • 待解决疑问:如下代码应当添加在项目的什么位置?
use Illuminate\Support\Facades\Auth;
Auth::logoutOtherDevices($currentPassword);

解答

将Auth::logoutOtherDevices($request->password);添加到登录认证成功后的执行逻辑中即可,通常是放在登录控制器的authenticated方法内,参考实现代码如下:

public function authenticated(Request $request)
{
    Auth::logoutOtherDevices($request->password);
    if (Auth::check() && Auth::user()->role_id == 1) {
        return redirect('/admin/dashboard');
    }
    $config = DB::table('config')->get();
    /*starts*/
    $id = Session::get('selected_plan_id');

    if(empty($id))
    {
        return redirect('/user/dashboard');
    }
    else
    {
        $selected_plan = Plan::where('plan_id', $id)->where('status', 1)->first();

        if ($selected_plan == null) {
            alert()->error('Your current plan is not available. Choose another plan.');
            return redirect('/user/plans');
        } else {

            if ($selected_plan == null) {
                return view('errors.404');
            } else {

                return redirect('/user/rediretoCheckout');
            }
        }
    }   
}

注意:logoutOtherDevices方法要求传入用户当前登录的明文密码作为校验参数,直接从登录请求对象中取$request->password传入即可,不需要额外做哈希处理。

内容的提问来源于stack exchange,提问作者kumari shwetha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 12:00:49