Skip to content

Commit c4305e1

Browse files
committed
docs(jwt): remove OpenAPI decorator for the token
1 parent e1686d6 commit c4305e1

File tree

1 file changed

+13
-101
lines changed

1 file changed

+13
-101
lines changed

core/jwt.md

Lines changed: 13 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -198,113 +198,25 @@ You must set the [JWT token](https://github.com/lexik/LexikJWTAuthenticationBund
198198

199199
### Adding endpoint to SwaggerUI to retrieve a JWT token
200200

201-
We can add a `POST /authentication_token` endpoint to SwaggerUI to conveniently retrieve the token when it's needed.
201+
LexikJWTAuthenticationBundle has an integration with API Platform to automatically
202+
add an OpenAPI endpoint to conveniently retrieve the token in Swagger UI.
202203

203-
![API Endpoint to retrieve JWT Token from SwaggerUI](images/jwt-token-swagger-ui.png)
204-
205-
To do it, we need to create a decorator:
206-
207-
```php
208-
<?php
209-
// api/src/OpenApi/JwtDecorator.php
210-
211-
namespace App\OpenApi;
212-
213-
use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface;
214-
use ApiPlatform\OpenApi\OpenApi;
215-
use ApiPlatform\OpenApi\Model;
216-
217-
final class JwtDecorator implements OpenApiFactoryInterface
218-
{
219-
public function __construct(
220-
private OpenApiFactoryInterface $decorated
221-
) {}
222-
223-
public function __invoke(array $context = []): OpenApi
224-
{
225-
$openApi = ($this->decorated)($context);
226-
$schemas = $openApi->getComponents()->getSchemas();
227-
228-
$schemas['Token'] = new \ArrayObject([
229-
'type' => 'object',
230-
'properties' => [
231-
'token' => [
232-
'type' => 'string',
233-
'readOnly' => true,
234-
],
235-
],
236-
]);
237-
$schemas['Credentials'] = new \ArrayObject([
238-
'type' => 'object',
239-
'properties' => [
240-
'email' => [
241-
'type' => 'string',
242-
'example' => 'johndoe@example.com',
243-
],
244-
'password' => [
245-
'type' => 'string',
246-
'example' => 'apassword',
247-
],
248-
],
249-
]);
250-
251-
$schemas = $openApi->getComponents()->getSecuritySchemes() ?? [];
252-
$schemas['JWT'] = new \ArrayObject([
253-
'type' => 'http',
254-
'scheme' => 'bearer',
255-
'bearerFormat' => 'JWT',
256-
]);
257-
258-
$pathItem = new Model\PathItem(
259-
ref: 'JWT Token',
260-
post: new Model\Operation(
261-
operationId: 'postCredentialsItem',
262-
tags: ['Token'],
263-
responses: [
264-
'200' => [
265-
'description' => 'Get JWT token',
266-
'content' => [
267-
'application/json' => [
268-
'schema' => [
269-
'$ref' => '#/components/schemas/Token',
270-
],
271-
],
272-
],
273-
],
274-
],
275-
summary: 'Get JWT token to login.',
276-
requestBody: new Model\RequestBody(
277-
description: 'Generate new JWT Token',
278-
content: new \ArrayObject([
279-
'application/json' => [
280-
'schema' => [
281-
'$ref' => '#/components/schemas/Credentials',
282-
],
283-
],
284-
]),
285-
),
286-
security: [],
287-
),
288-
);
289-
$openApi->getPaths()->addPath('/auth', $pathItem);
290-
291-
return $openApi;
292-
}
293-
}
294-
```
295-
296-
And register this service in `config/services.yaml`:
204+
If you need to modify the default configuration, you can do it in the dedicated configuration file:
297205

298206
```yaml
299-
# api/config/services.yaml
300-
services:
207+
# config/packages/lexik_jwt_authentication.yaml
208+
lexik_jwt_authentication:
301209
# ...
302-
303-
App\OpenApi\JwtDecorator:
304-
decorates: 'api_platform.openapi.factory'
305-
arguments: ['@.inner']
210+
api_platform:
211+
check_path: /auth
212+
username_path: email
213+
password_path: password
306214
```
307215

216+
You will see something like this in Swagger UI:
217+
218+
![API Endpoint to retrieve JWT Token from SwaggerUI](images/jwt-token-swagger-ui.png)
219+
308220
## Testing
309221

310222
To test your authentication with `ApiTestCase`, you can write a method as below:

0 commit comments

Comments
 (0)