1

I'm working with symfont and JSON Web Token and Symfony.

I'm trying to get jwt with ajax, but I get 401 (Unauthorized) The problem is with ajax, because i try with postman and I can get the token.

here is my security.yaml

security:
    encoders:
        App\Entity\Users:
            algorithm: bcrypt

    providers:
        users:
            entity:
                class: App\Entity\Users
                property: email

    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        login_app_manager:
            pattern:  ^/user/login
            stateless: true
            anonymous: true
            provider: users
            json_login:
                check_path: /user/login
                username_path: email
                password_path: password
                success_handler: lexik_jwt_authentication.handler.authentication_success
                failure_handler: lexik_jwt_authentication.handler.authentication_failure
                require_previous_session: false


        app_api_manager:
            pattern:  ^/mngr/api
            stateless: true
            anonymous: false
            provider: users
            guard:
                authenticators:
                    - lexik_jwt_authentication.jwt_token_authenticator

    access_control:
        - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/api, roles: IS_AUTHENTICATED_FULLY }


here is my ajax

var data = {
    'email':email,
    'password':password
};
$.ajax({
    type: "POST",
    url: "/user/login",
    contentType: "application/json",
    data: JSON.stringify(data),
    success: function(response) {
        console.log(response);
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        console.log('Error : ' + errorThrown);
    }
});

please help me, thank you

3
  • Can you provide your complete security.yml please ? Commented Apr 11, 2019 at 16:09
  • @kemicofa i already update the quesion with the complete security.yml Commented Apr 11, 2019 at 16:24
  • Try to change in access_control section in path ^/user/login instead of ^/login Commented Apr 12, 2019 at 11:44

2 Answers 2

1

Try post instead of ajax:

        $(document).ready(function{
            //form submit
            $("form").submit(function(event){
                var email      = $('#email').val();
                var password   = $('#password').val();
                $.post("/user/login",{
                    email:email, password: password
                }).done(function(data){

                })
            })
        });

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for your answe, i try and i still 401 (Unauthorized) and i don't have the jwt because that request is to ask it
1

In access_control add path

  • { path: ^/user/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }

instead of

  • { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }

and make sure to include this path in routes.yaml

user_login:
 path: /user/login
 methods: ['POST']
       

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.