I'm just trying to create my sf3 api using login using JWT, but I am having authentication issues. So, here are my configurations and some tests that I understood.
security.yml:
security:
firewalls:
login:
pattern: ^/api/auth
stateless: true
anonymous: true
form_login:
check_path: /api/auth/login-check
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
require_previous_session: false
api:
pattern: ^/api
stateless: true
lexik_jwt: ~
access_control:
- { path: ^/api/auth, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api, roles: IS_AUTHENTICATED_FULLY }
routing.yml:
auth:
path: /auth
defaults: { _controller: api.controller.auth:postAction }
methods: [OPTIONS, POST]
api_login_check:
path: /auth/login-check
config.yml:
nelmio_cors:
paths:
'^/api/':
allow_origin: ['*']
allow_headers: ['*']
allow_methods: ['POST', 'PUT', 'GET', 'DELETE', 'OPTIONS']
max_age: 3600
When I debug routing, I get this output:
$ php bin/console debug:router
----------------------------------- ------------------ -------- ------ -----------------------------------
Name Method Scheme Host Path
----------------------------------- ------------------ -------- ------ -----------------------------------
....
api_homepage ANY ANY ANY /api/
auth OPTIONS|POST ANY ANY /api/auth
api_login_check ANY ANY ANY /api/auth/login-check
....
So far so good, now there are problems.
I was able to verify the authentication return using
$ curl -v -X POST http://api.local/api/auth/login-check -d _username=user -d _password=user
Note: Unnecessary use of -X or
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to api.local (127.0.0.1) port 80 (
> POST /api/auth/login-check HTTP/1.1
> Host: api.local
> User-Agent: curl/7.51.0
> Accept: *
Great, it works!
When I try to use the same request in the browser, I get 404 in the OPTIONS method, so I try to recreate the request using curl OPTIONS request (really, I don’t know if it makes any sense, but I tried it anyway , where's the answer:
$ curl -v -X OPTIONS http://api.local/api/auth/login-check -d _username=user -d _password=user
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to api.local (127.0.0.1) port 80 (
> OPTIONS /api/auth/login-check HTTP/1.1
> Host: api.local
> User-Agent: curl/7.51.0
> Accept: *
, api OPTIONS, : [OPTIONS, POST] api_login_check, .
nelmioCorsBundle, .
, , ?