jeudi 26 mai 2016

Laravel 5 - How to logout a user from all of his devices using Redis

I'm building a laravel application where A user logged in other device and While logged out I want to force him to logged out from other device also. How do I implement in laravel.

Personally I have used Redis Server for that.But I don't know why It's not working. While running the program I have run redis-server.exe also.And I'm in Windows. I have changed the .env file also with CACHE_DRIVER=redis SESSION_DRIVER=redis And Here is my Controller where I have written the login for forcing the user from logged out from other device. If anyone find any solution please help me to find it out.Thanks In Advance.

Controller:

  public function postSignIn(Request $request)
  {       

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



public function getLogout()
{
    $redis = Redis::connection();
    $userId =Auth::user()->id;
    $userSessions = $redis->smembers('user:sessions:' . $userId);
    $redis->sadd('users:sessions:'.$userId,Session::getId());
    $currentSession = Session::getId();
    foreach ($userSessions as $sessionId) {
      if ($currentSession == $sessionId) {
        continue; 
      }
      $redis->srem('user:sessions:' . $userId, $sessionId);
      $redis->del('laravel:' . $sessionId);
      \Session::setId( $sessionId ); 
        \Session::clear();
    }
    Auth::logout();
    return redirect()->route('main');
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire