php Laravel JWT Auth 在登录时获取用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39869129/
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 JWT Auth get user on Login
提问by Jamie
Is it possible with https://github.com/tymondesigns/jwt-authto get the current user
? Because right now I can only generate a token (when a user sign in).
是否可以使用https://github.com/tymondesigns/jwt-auth获取current user
? 因为现在我只能生成一个令牌(当用户登录时)。
public function login(Request $request)
{
$credentials = $request->only('email', 'password');
try {
if (! $token = JWTAuth::attempt($credentials)) {
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (Tymon\JWTAuth\Exceptions\JWTException $e) {
return response()->json(['error' => 'could_not_create_token'], 500);
}
return response()->json(compact('token'));
}
回答by FrancescoMussi
You don't need Auth::user();
你不需要 Auth::user();
You can use the toUser
method of JWTAuth
. Just pass the token as parameter and you will get back the user info:
您可以使用 的toUser
方法JWTAuth
。只需将令牌作为参数传递,您就会得到用户信息:
$user = JWTAuth::toUser($token);
return response()->json(compact('token', 'user'));
For more info, this is the toUser method:
有关更多信息,这是 toUser 方法:
/**
* Find a user using the user identifier in the subject claim.
*
* @param bool|string $token
*
* @return mixed
*/
public function toUser($token = false)
{
$payload = $this->getPayload($token);
if (! $user = $this->user->getBy($this->identifier, $payload['sub'])) {
return false;
}
return $user;
}
回答by shijinmon Pallikal
You can get logged user data.
您可以获得记录的用户数据。
$credentials = $request->only('code', 'password', 'mobile');
try {
// verify the credentials and create a token for the user
if (! $token = JWTAuth::attempt($credentials)) {
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
// something went wrong
return response()->json(['error' => 'could_not_create_token'], 500);
}
$currentUser = Auth::user();
print_r($currentUser);exit;
回答by tomiplaz
You can also use JWTAuth::user()
method.
您也可以使用JWTAuth::user()
方法。
The user()
method call is returned in the toUser()
method, which itself is an alias for authenticate()
method which authenticates a user via a token. If the user is already authenticated, there is no need to authenticate them again (which toUser()
does), instead user()
method can be used to get the authenticated user.
所述user()
方法调用在返回的toUser()
方法,其本身是一个别名为authenticate()
用于认证经由令牌的用户的方法。如果用户已经通过身份验证,则无需再次对其进行身份验证(toUser()
确实如此),而是user()
可以使用方法来获取经过身份验证的用户。
// $token = JWTAuth::attempt($credentials) was successful...
$user = JWTAuth::user();
return response()->json(compact('token', 'user'));
回答by Mammadli Anar
It works fine for me (laravel 5.7) U must post token to me function and will return user use Tymon\JWTAuth\Facades\JWTAuth;
它对我来说很好用(laravel 5.7)你必须将令牌发布给我的功能,并将返回用户使用 Tymon\JWTAuth\Facades\JWTAuth;
public function me(Request $request)
{
$user = JWTAuth::user();
if (count((array)$user) > 0) {
return response()->json(['status' => 'success', 'user' => $user]);
} else {
return response()->json(['status' => 'fail'], 401);
}
}
回答by Mehdi Rezazadeh
just need to return inside the specified route middleware The route that you defined api route
只需要在指定的路由中间件里面返回你定义的路由 api 路由
Route::group(['middleware' => 'jwt.auth'], function () {
Route::post('/user', function (Request $request) {
try {
$user = \Illuminate\Support\Facades\Auth::user();
return $user;
} catch (\Tymon\JWTAuth\Exceptions\UserNotDefinedException $e) {
return '$user';
}
});
});
回答by bharat
try this (it works fine with laravel 5.6,5.7):
试试这个(它适用于 laravel 5.6,5.7):
use Tymon\JWTAuth\Facades\JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;
// code to generate token for user with email [email protected]
$user=User::where('email','=','[email protected]')->first();
if (!$userToken=JWTAuth::fromUser($user)) {
return response()->json(['error' => 'invalid_credentials'], 401);
}
return response()->json(compact('userToken'));
回答by MoadKey
You can get the current user related to token using :
您可以使用以下方法获取与令牌相关的当前用户:
$user = JWTAuth::setToken($token)->toUser();