Skip to content

Commit 328b202

Browse files
committed
added user id to JWT output
1 parent 72add8d commit 328b202

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

app/config/services.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ parameters:
44
# parameter_name: value
55

66
services:
7-
# service_name:
8-
# class: AppBundle\Directory\ClassName
9-
# arguments: ["@another_service_name", "plain_value", "%parameter_name%"]
7+
8+
event.jwt_created_listener:
9+
class: AppBundle\Event\Listener\JWTCreatedListener
10+
arguments:
11+
- "@security.token_storage"
12+
tags:
13+
- { name: kernel.event_listener, event: lexik_jwt_authentication.on_jwt_created, method: onJWTCreated }
1014

1115
user.mailer.rest:
1216
class: AppBundle\Mailer\RestMailer
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace AppBundle\Event\Listener;
4+
5+
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
6+
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
7+
8+
class JWTCreatedListener
9+
{
10+
/**
11+
* @var TokenStorageInterface
12+
*/
13+
private $tokenStorage;
14+
15+
/**
16+
* @param TokenStorageInterface $tokenStorage
17+
*/
18+
public function __construct( TokenStorageInterface $tokenStorage)
19+
{
20+
$this->tokenStorage = $tokenStorage;
21+
}
22+
23+
/**
24+
* Adds additional data to the generated JWT
25+
*
26+
* @param JWTCreatedEvent $event
27+
*
28+
* @return void
29+
*/
30+
public function onJWTCreated(JWTCreatedEvent $event)
31+
{
32+
$payload = $event->getData();
33+
34+
// add new data
35+
$payload['userId'] = $this->tokenStorage->getToken()->getUser()->getId();
36+
37+
$event->setData($payload);
38+
}
39+
}

0 commit comments

Comments
 (0)