Laravel 5.2 - How to logout a user from all of his devices(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 - 如何从用户的所有设备上注销
基础教程推荐
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 使用 PDO 转义列名 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01