@@ -198,113 +198,23 @@ You must set the [JWT token](https://github.com/lexik/LexikJWTAuthenticationBund
198
198
199
199
# ## Adding endpoint to SwaggerUI to retrieve a JWT token
200
200
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.
202
203
203
- 
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\O penApi;
212
-
213
- use ApiPlatform\O penApi\F actory\O penApiFactoryInterface;
214
- use ApiPlatform\O penApi\O penApi;
215
- use ApiPlatform\O penApi\M odel;
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 \A rrayObject([
229
- 'type' => 'object',
230
- 'properties' => [
231
- 'token' => [
232
- 'type' => 'string',
233
- 'readOnly' => true,
234
- ],
235
- ],
236
- ]);
237
- $schemas['Credentials'] = new \A rrayObject([
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 \A rrayObject([
253
- 'type' => 'http',
254
- 'scheme' => 'bearer',
255
- 'bearerFormat' => 'JWT',
256
- ]);
257
-
258
- $pathItem = new Model\P athItem(
259
- ref: 'JWT Token',
260
- post: new Model\O peration(
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\R equestBody(
277
- description: 'Generate new JWT Token',
278
- content: new \A rrayObject([
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
+ To enable it, modify the following configuration :
297
205
298
206
` ` ` yaml
299
- # api/ config/services .yaml
300
- services :
207
+ # config/packages/lexik_jwt_authentication .yaml
208
+ lexik_jwt_authentication :
301
209
# ...
302
-
303
- App\O penApi\J wtDecorator:
304
- decorates: 'api_platform.openapi.factory'
305
- arguments: ['@.inner']
210
+ api_platform:
211
+ check_path: /auth
306
212
` ` `
307
213
214
+ You will see something like this in Swagger UI :
215
+
216
+ 
217
+
308
218
# # Testing
309
219
310
220
To test your authentication with `ApiTestCase`, you can write a method as below :
0 commit comments