LexikJWT通过令牌获取用户档案

fef*_*efe 2 profile symfony lexikjwtauthbundle

使用LexikJWTAuthenticationBundle,FOSRest,FOSUser如何通过令牌获取经过身份验证的用户配置文件.可能吗?

所以,假设用户已经通过LexikJWT进行了身份验证,并且我有一个api端点,就像/api/profile我发送令牌一样,我希望得到指定的用户数据.

我正在使用Redux的前端ReactJS.

LBA*_*LBA 7

这是在用户已经过身份验证后如何通过服务获取用户的示例:

class UserService
{

    /** @var  TokenStorageInterface */
    private $tokenStorage;

    /**
     * @param TokenStorageInterface  $storage
     */
    public function __construct(
        TokenStorageInterface $storage,
    )
    {
        $this->tokenStorage = $storage;
    }

    public function getCurrentUser()
    {
        $token = $this->tokenStorage->getToken();
        if ($token instanceof TokenInterface) {

            /** @var User $user */
            $user = $token->getUser();
            return $user;

        } else {
            return null;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在你的services.yml:

tenant_user_service:
    class: YourBundle\YourPackage\UserService
    arguments: [ '@security.token_storage' ]
Run Code Online (Sandbox Code Playgroud)

这将返回您的用户 - 但请注意,根据用户在身份验证期间设置为令牌的方式,这也可以仅将您的用户名作为字符串.但基本上你从当前的$ token-> getUser()获得任何内容.