沃梦达 / 编程问答 / php问题 / 正文

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

Laravel 5.2 - How to logout a user from all of his devices(Laravel 5.2 - 如何从用户的所有设备上注销)

本文介绍了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');
}

这篇关于Laravel 5.2 - 如何从用户的所有设备上注销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Laravel 5.2 - 如何从用户的所有设备上注销

基础教程推荐