Lexikjwtauthenticationbundle: Getting logged in user

Created on 8 Jun 2017  路  7Comments  路  Source: lexik/LexikJWTAuthenticationBundle

I managed to create a token and pass it as header, and the JWT recognize it. But the token saved in token.storage does not contains any information about the user.

I have this:
JWTUserToken {#162 #rawToken: "eyJhbGciOiJSUzI1NiJ9.eyJyb2xlcyI6WyJST0xFX1VTRVIiXSwiZW1haWwiOiJ1c2VyQHJhZGl4YXIuY29tIiwiaWF0IjoxNDk2OTU1NzgzLCJleHAiOjE0OTk1NDc3ODN9.esW0MHTrofB2czMmloaUHkaeyhe7QCF0IMdh4BOmRROvS0mK8bmC2To4qH5aVqiYNCjJXxz68t0Mpp-Q_AO0GyFLiYa6-6U3uVt6k3qo0DqVYeIVKdQ4Fu8miu1-G02RP7BURikaSrKQQ6Rxm7awvlzGtHglp3jFuazhD-nD_EG8udAMapzVQyMu1B_gc8TG5MAxiWmuOo2Q85V_XBDa5vZhDrglXeZUdGlmvCacD2x3DeLt6a_w-rkr_heu8daV-AiRhrY1iHufvdOEgAlzWisKwKuwWvbjDfvY9O0RxWfj-zzwMBPoMwo0mUfOpUAo0RWBdD7iNH4L7XJ5gpBMjwuYWBIDVw9KG57KAOl7_nIgN9F89sRhgaBj9qWdTrS--vNHpoVK2pdxhaOik3Uu8TlYgqVIhqob832cVXGUnHJ-EkD3jnMHEsH36XeCoZIQ0xbSQr4njCid5MMfqqWzqxEIvqbiVrmdpjnqCCDK4EIal2QOmYlBgbk2fxmcWWwP0wewRFY15bJXOKinZ1rsQmyj7pbScPZYCVRZP65P1-aoT3-S90tG5iPNaFYGmc-dMdLVdsHQXoqcbj55qU0j-92hEWVUDJbLz28D6rH-yXyMQvu4SSSRPpL3qpBqJ1ycbBDWwkqZsftTdUtlYyVQR5YIhqkMnyXtKAEc1NGYRYg" #providerKey: null -user: null -roles: [] -authenticated: true -attributes: [] }

How do i get the user?

Bug Unconfirmed

Most helpful comment

@pbalan good to hear.

I must say that calling authenticators from a controller is not sane. There are built-in mechanisms for securing your symfony application, the bundle's docs show how to leverage it.

Authorizing/authenticating is not the role of your controllers at all, doing so will likely produce such weird issues as it probably won't have the full logic of the security system such as token storage, dispatching events you may want to hook on (or another developer taking the project over after you), exception message treatment (skipping it leads to true security holes that symfony's tries hard to avoid for years).
The authenticator needs to be properly used for your application to be fine, Guard does a great job on that, that is the reason for which we chose it.

Please consider verifying that the way you are using this bundle is the documented one before opening an issue. We don't want to spent time at reproducing/trying to fix use cases that aren't officially supported, because they are bad practices.

All 7 comments

That's weird, I'm looking into.

you can use https://jwt.io/ for inspect your token.

You token contain
{
"roles": [
"ROLE_USER"
],
"email": "[email protected]",
"iat": 1496955783,
"exp": 1499547783
}

@chalasr Hello je te vois partout sur les post symfony !!

@jmogilner I can't reproduce, the security token contains the correct information. Are you extending the authenticator somehow?

@villers hello :)

@jmogilner Have the same issue. Can you share your configuration and do you make any changes on your entity side? Also, how do you try get user in controller?

Could one of you provide a reproducer (e.g. forking the symfony/symfony-standard and make the code changes that are necessary to reproduce)? That would be best.

@chalasr, I fixed my issue. Great Bundle!!
The fix was simple. I had to use the following two lines in my controller to fetch the user.

$preAuthToken = $this->jwtAuthenticator->getCredentials($request);
$response['user'] = $this->jwtManager->decode($preAuthToken);

@pbalan good to hear.

I must say that calling authenticators from a controller is not sane. There are built-in mechanisms for securing your symfony application, the bundle's docs show how to leverage it.

Authorizing/authenticating is not the role of your controllers at all, doing so will likely produce such weird issues as it probably won't have the full logic of the security system such as token storage, dispatching events you may want to hook on (or another developer taking the project over after you), exception message treatment (skipping it leads to true security holes that symfony's tries hard to avoid for years).
The authenticator needs to be properly used for your application to be fine, Guard does a great job on that, that is the reason for which we chose it.

Please consider verifying that the way you are using this bundle is the documented one before opening an issue. We don't want to spent time at reproducing/trying to fix use cases that aren't officially supported, because they are bad practices.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JuliusKoronci picture JuliusKoronci  路  4Comments

christophe-mailfert picture christophe-mailfert  路  5Comments

AlexDupreWeb picture AlexDupreWeb  路  4Comments

pbalan picture pbalan  路  5Comments

garak picture garak  路  5Comments