Laravel 5.2 - 如何从用户的所有设备上注销

2021-12-28 00:00:00 redis session php laravel laravel-5.2

当用户从特定设备注销时,我想从他迄今为止登录的所有设备注销.我如何在 Laravel 中做到这一点.

When a user logged out from a perticular device I want to logout from all the device he has logged in till now . How I do it in Laravel.

我使用Redis通过安装predis/predis":~1.0"

I have used Redis for keeping the userId in Session by installing "predis/predis": "~1.0"

这是我的登录和注销控制器:

And Here is my controller for SignIn and Logout:

  public function postSignIn(Request $request)
    {       

       if (Auth::attempt(['email' => $request['email'], 'password' =>$request['password'] ]) ) {

       $redis = Redis::connection();   
        $userId=Session::getId();
        $redis->sadd('users:sessions:'.$userId,Session::getId());
          return redirect()->route('main');

        }
        return redirect()->back();
    }



public function getLogout()
{
    $redis = Redis::connection();
    $userId=Session::getId();
    $userSessions = $redis->smembers('user:sessions:' . $userId);
    $currentSession = Session::getId();
    foreach ($userSessions as $sessionId) {
         if ($currentSession == $sessionId) {
      continue; 

            }
             $redis->srem('user:sessions:' . $userId, $sessionId);
            $redis->del('laravel:' . $sessionId);

        }
    Auth::logout();
    return redirect()->route('main');
}

它已成功登录并注销,但不会终止其他设备中的所有会话.

It's successfully get logged in and also logged out but it doesn't kill all the session in other devices.

我该如何解决问题?

推荐答案

所以问题是 redis 键名输入错误,用于写入数据$redis->sadd('users:sessions:'.$userId,Session::getId());其中键的前缀 'users:sessions:' 和用于获取数据$redis->srem('user:sessions:' . $userId, $sessionId); where key 的前缀 'user:sessions:'这就是代码不起作用并且 dd() 返回空数组的原因.

So issue was with typo in redis key name, for write data used $redis->sadd('users:sessions:'.$userId,Session::getId()); where key's prefix 'users:sessions:' and for get data used $redis->srem('user:sessions:' . $userId, $sessionId); where key's prefix 'user:sessions:' Thats why code didn't work and dd() returned empty array.

正确的代码看起来像这样

so correct code looks like this

public function postSignIn(Request $request)
{    

   if (Auth::attempt(['email' => $request['email'], 'password' =>$request['password'] ]) ) {
        $redis = Redis::connection();   
        $userId=Session::getId();
        $redis->sadd('user:sessions:'.$userId,Session::getId());
        return redirect()->route('main');
    }
    return redirect()->back();
}



public function getLogout()
{
    $redis = Redis::connection();
    $userId=Session::getId();
    $userSessions = $redis->smembers('user:sessions:' . $userId);
    $currentSession = Session::getId();

    foreach ($userSessions as $sessionId) {
         if ($currentSession == $sessionId) {
             continue; 
         }
            $redis->srem('user:sessions:' . $userId, $sessionId);
            $redis->del('laravel:' . $sessionId);
        }
    Auth::logout();
    return redirect()->route('main');
}

相关文章