在 Laravel/Lumen 上使用 JWT 进行身份验证时如何获取 $user 对象?
How to get the $user object when authenticated with JWT on Laravel/Lumen?
我有一个使用 JWT 进行身份验证的应用程序。这是我第一次创建。当我试图在一个函数中获取用户对象时,我找不到方法去做。这是我的代码:
public function getEmail(\Illuminate\Http\Request $request)
{
$user = \Auth::user();
return new JsonResponse(['message' => $user->email]);
}
returns $user 对象为空,可能是因为 Auth class 与会话身份验证有关。我正在使用这个样板:https://github.com/krisanalfa/lumen-jwt
我查看了代码,但找不到获取用户的方法,有人可以帮忙吗?
你试过使用这个功能吗?
JWTAuth::user();
据此Guide for setting up with Lumen
您可以从请求对象中获取它:
示例路线:
$app->get('/login', function (Request $request) {
$token = app('auth')->attempt($request->only('email', 'password'));
return response()->json(compact('token'));
});
$app->get('/me', function (Request $request) {
return $request->user();
});
找了几个小时后,我找到了一个可行的答案:
$user = app('Dingo\Api\Auth\Auth')->user();
我有一个使用 JWT 进行身份验证的应用程序。这是我第一次创建。当我试图在一个函数中获取用户对象时,我找不到方法去做。这是我的代码:
public function getEmail(\Illuminate\Http\Request $request)
{
$user = \Auth::user();
return new JsonResponse(['message' => $user->email]);
}
returns $user 对象为空,可能是因为 Auth class 与会话身份验证有关。我正在使用这个样板:https://github.com/krisanalfa/lumen-jwt
我查看了代码,但找不到获取用户的方法,有人可以帮忙吗?
你试过使用这个功能吗?
JWTAuth::user();
据此Guide for setting up with Lumen
您可以从请求对象中获取它:
示例路线:
$app->get('/login', function (Request $request) {
$token = app('auth')->attempt($request->only('email', 'password'));
return response()->json(compact('token'));
});
$app->get('/me', function (Request $request) {
return $request->user();
});
找了几个小时后,我找到了一个可行的答案:
$user = app('Dingo\Api\Auth\Auth')->user();