Symfony2 chain_provider in_memory users log in "bad credentials"

php, symfony

Solution

I encountered the same problem, I'm adding the answer to make it more visible to others. If you get

No encoder has been configured for account "Symfony\Component\Security\Core\User\User" chain provider

You need to add the encoder like it's shown below (for the database users and the users in memory)[security.yml]

security:
    encoders:
        Splendonia\Bundle\UserBundle\Entity\User:
            algorithm: sha512
            encode-as-base64: true
            iterations: 10
        Symfony\Component\Security\Core\User\User: plaintext

To use one or more providers. The docs are here and it looks like this:

providers:
        chain_provider:
            chain:
                providers: [in_memory, main]
        in_memory:
            memory:
                users:
                    guest:  { password: guest, roles: [ 'ROLE_GUEST' ] }
        main:
            entity: { class: Splendonia\Bundle\UserBundle\Entity\User, property: email }

Also remember to change the provider to chain_provider on firewalls, it looks something like this:

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

        login:
            pattern:  ^/demo/secured/login$
            security: false

        secured_area:
            pattern:    ^/
            anonymous: ~
            provider: chain_provider
            form_login:
                login_path: login
                check_path: login_check
                default_target_path: /dashboard

            logout:
                path:   /logout
                target: /

and that should do it.

Problem

I want to have one hard coded admin user and the rest users coming from database. When I login with db users, it works, but if I login with hard coded admin user, it shows "Bad credentials" error. Here is a part of my security.yml file: ``` security: encoders: Valoran\DrushBundle\Entity\User: algorithm: bcrypt cost: 15 role_hierarchy: ROLE_ADMIN: ROLE_USER ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH] providers: chain_provider: chain: providers: [in_memory, user_db] in_memory: memory: users: foo: { password: test } user_db: entity: { class: Acme\DrushBundle\Entity\User, property: userName } ```

Original source