New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refresh token with additional fields in payload #65
Comments
I have the same problem like you. How did you handle it @nicraMarcin ? This issue still exist. Did you use an other bundle or customize this one? |
@thecassion I created my own custom refresh controller
now in frontend before token expires I make request to this route. |
Ok . Thanks @nicraMarcin I understand . For me I don't use FOSRestBundle . I only use Api-platform . I will try to refresh token with my own logic like you do. Thanks again |
I use this with Api Platform and it works. I use another Event I think: use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
use MWS\UserBundle\Model\UserInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class JWTAuthenticatedListener
{
/**
* @var TokenStorageInterface
*/
private $tokenStorage;
/**
* @param TokenStorageInterface $tokenStorage
*/
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
/**
*
* @param AuthenticationSuccessEvent $event
* @return void
*/
public function onAuthenticationSuccessResponse(AuthenticationSuccessEvent $event)
{
$user = $event->getUser();
if (!$user instanceof UserInterface) {
return;
}
$data = $event->getData();
$data['id'] = $user->getId();
$event->setData($data);
}
} This does not encrypt the payload in the token but send the data with it. Encrypting additional data in the token wasn't possible. Dont forget to give the listener a lower priority. The solution i found and helped me was this: #67 |
Using api-platform, I had the same problem and had to declare a custom user provider in the gesdinet_jwt_refresh_token.yaml configuration file (had to create this file in /config/packages) :
Then it used my User's entity in the onJWTCreated event and not the default one, and I had access to my custom method (like getId()) |
FWIW, I was also having an issue with the JWT generated after refresh. My issue was that on login, the JWT would contain the complete set of User roles, but on refresh, it wouldn't. The fix was simply to specify the # gesdinet_jwt_refresh_token.yaml
gesdinet_jwt_refresh_token:
user_provider: fos_user.user_provider.username_email |
@wittyweb You saved me there! Thanks! |
@jspizziri This should be in the docs! Thanks! |
Hello, I have the same issue but i'm not using custom provider; can you help me please ? I'mmmm so stuck :') |
@steveKac01 i haven't used symfony in several years. Good luck! |
@jspizziri @steveKac01 this is my first participation, I hope to help you 😉 # config/packages/security.yaml
app_user_provider: # the provider i use for refresh token
entity:
class: App\Entity\User
property: username
jwt:
lexik_jwt:
class: App\Security\UserAuthenticate
firewalls:
dev:
pattern: ^/_(profiler|wdt)
security: false
login:
# some config
api:
pattern: ^/api/
stateless: true
entry_point: jwt
provider: jwt
jwt: ~
refresh_jwt:
check_path: /api/token/refresh
provider: app_user_provider # the user's provider must be declared here # config/packages/gesdinet_jwt_refresh_token.yaml (automatically create from JWTRefreshTokenBundle recipes)
gesdinet_jwt_refresh_token:
refresh_token_class: @@App\Entity\RefreshToken |
Hello,
I'm new in symfony and and try to set HWTRefreshTokenBundle with JTW Lexik.
In jwt I have own Entity
In listener I add custom data:
But when I try to refresh token I get error:
When I throw email and name from payload token is created but without this data, and return only default role
ROLE_USER
withoutROLE_ADMIN
.I'tried to use
refresh_token_entity: SharedBundle\Security\User
but without result :/How can I implemet additional fields in payload?
The text was updated successfully, but these errors were encountered: