Symfony 5 – User login – No database request at login so getUser always null

I want to login through an API, based on my entity AppEntityUser.

Here is the security.yaml file:

security:
    encoders:
        AppEntityUser:
            algorithm: bcrypt

    providers:
        app_user_provider:
            entity:
                class: AppEntityUser
                property: username
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            pattern: ^/
            anonymous: ~
            provider: app_user_provider
            json_login:
                login_path: app_login
                check_path: app_login
                username_path: username
                password_path: password

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

My User entity implements UserInterface.

I have the following login action:

    /**
     * @Route("/login", methods={"POST"}, name="app_login")
     */
    public function login() : JsonResponse
    {
        return new JsonResponse([]);
    }

My actions is called correctly when I execute a HTTP request on the /login route with username and password passed as application/x-www-form-urlencoded data.

My problem is that checking the profiler, I can see there is no Doctrine request executed when calling /login. As a result, $this->getUser() always returns null if I try to call it from the login action.

When creating the Symfony project, I selected the "minimalist" project option, dedicated to APIs and micro-services, with fewer dependencies. Perhaps I also simply miss a package ?

The dependencies I have:

"require": {                                            
    "php": ">=7.2.5",                                   
    "ext-ctype": "*",                                   
    "ext-iconv": "*",                                   
    "composer/package-versions-deprecated": "1.11.99.1",
    "doctrine/annotations": "^1.11",                    
    "doctrine/doctrine-bundle": "^2.2",                 
    "doctrine/doctrine-migrations-bundle": "^3.0",      
    "doctrine/orm": "^2.7",                             
    "ramsey/uuid-doctrine": "^1.6",                     
    "symfony/console": "5.1.*",                         
    "symfony/dotenv": "5.1.*",                          
    "symfony/flex": "^1.3.1",                           
    "symfony/form": "5.1.*",                            
    "symfony/framework-bundle": "5.1.*",                
    "symfony/security-bundle": "5.1.*",                 
    "symfony/security-core": "5.1.*",                   
    "symfony/validator": "5.1.*",                       
    "symfony/yaml": "5.1.*"                             
},                                                      

Thanks in advance.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *