Laravel - 注销特定用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24282546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - log out specific user
提问by Onion
I know how to log in a user, but how do I log out a specified user from the application? There doesn't seem to be enough coverage on this.
我知道如何登录用户,但如何从应用程序中注销指定用户?似乎没有足够的覆盖面。
回答by Kamil Kie?czewski
This problem occures when you are admin and want to block some user. Then when you block user you want to logout him imidietly. For laravel 5.2 (maby for lower versions too) you can create middelware:
当您是管理员并想要阻止某些用户时会出现此问题。然后,当您阻止用户时,您想 imidietly 注销他。对于 laravel 5.2(也可以用于较低版本),您可以创建中间件:
Create middelware
创建中间件
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class BockedUser
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
$user = Auth::user();
if ($user and $user->is_bocked) {
Auth::logout();
return redirect('/login');
}
return $next($request);
}
}
And in app/Http/Kernel.php in section $middlewareGroups > 'web' add \App\Http\Middleware\BockedUser::class. I assmue that all your routes are in Route::group(['middleware' => 'web'], function () { .. all your routes ..}
并在 app/Http/Kernel.php 部分 $middlewareGroups > 'web' 添加 \App\Http\Middleware\BockedUser::class。我假设你所有的路线都在Route::group(['middleware' => 'web'], function () { .. all your routes ..}
回答by Shrikant Bhardwaj
You can do this for logout specific user logout in laravel 4.2 and you are using multi auth
您可以在 laravel 4.2 中为注销特定用户注销执行此操作,并且您正在使用多重身份验证
/* for normal user logout */
Auth::user()->logout();
/* for admin user logout */
Auth::admin()->logout();
/* for manager user logout */
Auth::manager()->logout();
as you made the users auth
当您让用户进行身份验证时
回答by Imad Ullah
Use the setUser to find a soluion
使用 setUser 查找解决方案
get current user
获取当前用户
$user = Auth::user();
logout user you want
登出你想要的用户
$userToLogout = User::find(5);
Auth::setUser($userToLogout);
Auth::logout();
set again current user
重新设置当前用户
Auth::setUser($user);
回答by metylbk
You can logout user when he access any authenticated function such as edit profile, edit post, create post, etc. For example:
您可以在用户访问任何经过身份验证的功能(例如编辑个人资料、编辑帖子、创建帖子等)时注销用户。例如:
public function edit()
{
if (!\Auth::user()->active)
{
\Auth::logout();
return redirect('/');
}
// code here
}