Laravel 应用中一个用户只允许单一会话存在

Laravel

Laravel 5.3 中使用 make:auth 生成的登录方案。

  1. User 模型对应的数据库表增加 session_id 字段;

  2. 修改 /app/Http/Controllers/Auth/LoginController.php,在顶部加上:
use Illuminate\Http\Request;
use Auth;

然后新增 authenticated 方法:

public function authenticated(Request $request,User $user){
    $previous_session = $user->session_id;

    if ($previous_session) {
        \Session::getHandler()->destroy($previous_session);
    }

    Auth::user()->session_id = \Session::getId();
    Auth::user()->save();
    return redirect()->intended($this->redirectPath());
}
评论数量: 0

20
点赞
696
浏览
0
评论

贡献 535
Summer
Building Products Users Love