gpt4 book ai didi

laravel - 如何使用具有不同用户表的多重身份验证注销 JWT token

转载 作者:行者123 更新时间:2023-12-04 10:07:35 24 4
gpt4 key购买 nike

这是 config/auth.php 中的代码

'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],

'api' => [
'driver' => 'jwt',
'provider' => 'users',
'hash' => false,
],
'client' => [
'driver' => 'jwt',
'provider' => 'clients',
]
],

'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'clients' => [
'driver' => 'eloquent',
'model' => App\Client::class,
],
],

我的登录功能对于不同的登录用户使用此代码没有问题:
config()->set('auth.defaults.guard', 'client');
Config::set('jwt.user', 'App\Client');
Config::set('auth.providers.clients.model', Client::class);

这是我的注销功能:
function logout(Request $request)
{
$this->validate($request, ['token' => 'required']);
Config::set('jwt.user', 'App\Client');
Config::set('auth.providers.clients.model', Client::class);
try {
JWTAuth::invalidate(JWTAuth::getToken());
return response()->json(['success' => true]);
} catch (JWTException $e) {
return response()->json(['success' => false, 'error' => 'Failed to logout'], 500);
}
}

我正在使用 Laravel 6 和 tymon/jwt-auth "1.0.0-rc.5.1"

最佳答案

如果你有一个用户和客户端的 token 表,那么你可以这样做......对于注销,你只需要从 token 表中删除 token ......简单的方法=>

public function logOut($id) {
if (Auth::check()) {
$user = Auth::user();
Token::where('user_id',$user->id)
->where(external identification)
->delete();
$response = [
'status' => true,
'message' => 'Successfully Logged Out',
];
return response()->json($response); //@ sending response
} else {
$response = [
'status' => false,
'message' => 'Bad Request!',
];
return response()->json($response); //@ sending response
}
}

关于laravel - 如何使用具有不同用户表的多重身份验证注销 JWT token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61494765/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com