@@ -15,7 +15,7 @@ We begin by installing the bundle:
15
15
16
16
``` console
17
17
docker compose exec php \
18
- composer require jwt-auth
18
+ composer require lexik/ jwt-authentication-bundle
19
19
```
20
20
21
21
Then we need to generate the public and private keys used for signing JWT tokens. If you're using the [ API Platform distribution] ( ../distribution/index.md ) , you may run this from the project's root directory:
@@ -69,7 +69,7 @@ security:
69
69
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
70
70
providers :
71
71
# used to reload user from session & other features (e.g. switch_user)
72
- app_user_provider :
72
+ users :
73
73
entity :
74
74
class : App\Entity\User
75
75
property : email
@@ -80,27 +80,28 @@ security:
80
80
security : false
81
81
main :
82
82
stateless : true
83
- provider : app_user_provider
83
+ provider : users
84
84
json_login :
85
- check_path : /authentication_token
85
+ check_path : auth # The name in routes.yaml is enough for mapping
86
86
username_path : email
87
87
password_path : password
88
88
success_handler : lexik_jwt_authentication.handler.authentication_success
89
89
failure_handler : lexik_jwt_authentication.handler.authentication_failure
90
90
jwt : ~
91
91
92
92
access_control :
93
- - { path: ^/docs, roles: PUBLIC_ACCESS } # Allows accessing the Swagger UI
94
- - { path: ^/authentication_token, roles: PUBLIC_ACCESS }
93
+ - { path: ^/$, roles: PUBLIC_ACCESS } # Allows accessing the Swagger UI
94
+ - { path: ^/docs, roles: PUBLIC_ACCESS } # Allows accessing the Swagger UI docs
95
+ - { path: ^/auth, roles: PUBLIC_ACCESS }
95
96
- { path: ^/, roles: IS_AUTHENTICATED_FULLY }
96
97
` ` `
97
98
98
- You must also declare the route used for ` /authentication_token `:
99
+ You must also declare the route used for ` /auth `:
99
100
100
101
` ` ` yaml
101
102
# api/config/routes.yaml
102
- authentication_token :
103
- path: /authentication_token
103
+ auth :
104
+ path: /auth
104
105
methods: ['POST']
105
106
` ` `
106
107
@@ -126,7 +127,7 @@ security:
126
127
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
127
128
providers:
128
129
# used to reload user from session & other features (e.g. switch_user)
129
- app_user_provider :
130
+ users :
130
131
entity:
131
132
class: App\E ntity\U ser
132
133
property: email
@@ -138,19 +139,20 @@ security:
138
139
api:
139
140
pattern: ^/api/
140
141
stateless: true
141
- provider: app_user_provider
142
+ provider: users
142
143
jwt: ~
143
144
main:
144
145
json_login:
145
- check_path: /authentication_token
146
+ check_path: auth # The name in routes.yaml is enough for mapping
146
147
username_path: email
147
148
password_path: password
148
149
success_handler: lexik_jwt_authentication.handler.authentication_success
149
150
failure_handler: lexik_jwt_authentication.handler.authentication_failure
150
151
151
152
access_control:
152
- - { path: ^/docs, roles: PUBLIC_ACCESS } # Allows accessing API documentations and Swagger UI
153
- - { path: ^/authentication_token, roles: PUBLIC_ACCESS }
153
+ - { path: ^/$, roles: PUBLIC_ACCESS } # Allows accessing the Swagger UI
154
+ - { path: ^/docs, roles: PUBLIC_ACCESS } # Allows accessing API documentations and Swagger UI docs
155
+ - { path: ^/auth, roles: PUBLIC_ACCESS }
154
156
- { path: ^/, roles: IS_AUTHENTICATED_FULLY }
155
157
` ` `
156
158
@@ -162,8 +164,6 @@ lexik_jwt_authentication:
162
164
secret_key: '%env(resolve:JWT_SECRET_KEY)%'
163
165
public_key: '%env(resolve:JWT_PUBLIC_KEY)%'
164
166
pass_phrase: '%env(JWT_PASSPHRASE)%'
165
-
166
- user_identity_field: email # Or the field you have setted using make:user
167
167
` ` `
168
168
169
169
# # Documenting the Authentication Mechanism with Swagger/Open API
@@ -286,7 +286,7 @@ final class JwtDecorator implements OpenApiFactoryInterface
286
286
security: [],
287
287
),
288
288
);
289
- $openApi->getPaths()->addPath('/authentication_token ', $pathItem);
289
+ $openApi->getPaths()->addPath('/auth ', $pathItem);
290
290
291
291
return $openApi;
292
292
}
@@ -339,7 +339,7 @@ class AuthenticationTest extends ApiTestCase
339
339
$manager->flush();
340
340
341
341
// retrieve a token
342
- $response = $client->request('POST', '/authentication_token ', [
342
+ $response = $client->request('POST', '/auth ', [
343
343
'headers' => ['Content-Type' => 'application/json'],
344
344
'json' => [
345
345
0 commit comments