沃梦达 / 编程问答 / php问题 / 正文

Laravel JWT Auth 在登录时获取用户

Laravel JWT Auth get user on Login(Laravel JWT Auth 在登录时获取用户)

本文介绍了Laravel JWT Auth 在登录时获取用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 https://github.com/tymondesigns/jwt-auth获取当前用户?因为现在我只能生成一个令牌(当用户登录时).

Is it possible with https://github.com/tymondesigns/jwt-auth to get the current user? Because right now I can only generate a token (when a user sign in).

public function login(Request $request)
{
    $credentials = $request->only('email', 'password');

    try {
        if (! $token = JWTAuth::attempt($credentials)) {
            return response()->json(['error' => 'invalid_credentials'], 401);
        }
    } catch (TymonJWTAuthExceptionsJWTException $e) {
        return response()->json(['error' => 'could_not_create_token'], 500);
    }

    return response()->json(compact('token'));
}

推荐答案

可以获取登录的用户数据.

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;

这篇关于Laravel JWT Auth 在登录时获取用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Laravel JWT Auth 在登录时获取用户

基础教程推荐