title: Symfony 3.3, authentication, bcrypt, bad credentials
body: <p>I am developing a Symfony 3.3 web app which includes a few bundles. </p>
  
  <p>I am trying to define a way to register an admin user in the databases as it follows. I am able to create the user in the database, but afterward I am not able to login as that user: I always get bad credentials. Although I am aware that things could be arranged in better ways, I would like to understand what I fail to see. </p>
  
  <p>So I have an AdminBundle, therein I have defined a User class:</p>
  
  <pre><code>&lt;?php
  
  namespace myApp\AdminBundle\Entity;
  
  
  use FOS\UserBundle\Model\User as BaseUser;
  use Doctrine\ORM\Mapping as ORM;
  
  
  /**
   * @ORM\Entity
   * @ORM\Table(name="fos_user")
   */
  class User extends BaseUser
  {
      /**
       * @ORM\Id
       * @ORM\Column(type="integer")
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      protected $id;
  
      public function __construct()
      {
          parent::__construct();
  
      }
  }
  ?&gt;
  </code></pre>
  
  <p>and a registerAction</p>
  
  <pre><code>    /**
      * register main user/admin
      *
      * @Route("/register/{nameuser}", name="Jus_Admin_register")
      * @Method("GET")
      * @Template()
      */
      public function registerAction($nameuser)
          {
  
      $user = new User();
      $user-&gt;setUsername($nameuser);
      $user-&gt;setEmail('aaa@aaa.aaa');
      $plainPassword = 'pw';
      $encoder = $this-&gt;get('security.encoder_factory')-&gt;getEncoder($user);
      $encoded = $encoder-&gt;encodePassword($user, $plainPassword);
  
      $user-&gt;setPassword($encoded);
      $user-&gt;setEnabled(TRUE);
      $user-&gt;setSuperAdmin(TRUE);
      $userManager = $this-&gt;container-&gt;get('fos_user.user_manager');
      $userManager-&gt;updateUser($user);
  
       return $this-&gt;render('myAppAdminBundle:Admin:login.html.twig');      
  }
  </code></pre>
  
  <p>My security.yml  has:</p>
  
  <pre><code>security:
      encoders:
          myApp\AdminBundle\Entity\User:
              algorithm: bcrypt
              cost: 10
              iterations: 1
  
     role_hierarchy:
          ROLE_ADMIN:       ROLE_USER
          ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
  
     providers:
         our_db_provider:
              entity:
                 class: myApp\AdminBundle\Entity\User
                 property: username
  ....
     firewalls:
         admin_area:
             pattern:   ^/myApp/Admin
             anonymous: ~
             form_login:
                login_path: /myApp/Admin/login
                check_path: /myApp/Admin/login_check
             logout:
                path:   /myApp/Admin/logout
                target: /myApp/Admin/admin
             http_basic:
                realm: "Admin reserved"
  
             context: primary_auth
  </code></pre>
  
  <p>and my config.yml has (indeed I am not sure whether I need the following at all):</p>
  
  <pre><code>fos_user:
     db_driver: orm # other valid values are 'mongodb' and 'couchdb'
     firewall_name: admin_area
     user_class: myApp\AdminBundle\Entity\User\
     from_email:
         address: "%mailer_user%"
         sender_name: "%mailer_user%"
  </code></pre>
  
  <p>If I call <code>http://localhost/myApp/Admin/register/admin</code>, the following record is inserted into the table fos_user:</p>
  
  <pre><code>--
  -- Dumping data for table `fos_user`
  --
  
  INSERT INTO `fos_user` (`id`, `username`, `username_canonical`, `email`, `email_canonical`, `enabled`, `salt`, `password`, `last_login`, `confirmation_token`, `password_requested_at`, `roles`) VALUES
  (1, 'admin', 'admin', 'aaa@aaa.aaa', 'aaa@aaa.aaa', 1, NULL, '$2y........yq', NULL, NULL, NULL, 'a:1:{i:0;s:16:\"ROLE_SUPER_ADMIN\";}');
  </code></pre>
  
  <p>However, I am not able to login. Logs says:</p>
  
  <blockquote>
    <p>[2017-08-.. ....] security.INFO: Authentication request failed. {"exception":"[object] (Symfony\Component\Security\Core\Exception\BadCredentialsException(code: 0): Bad credentials. at /..../vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Provider/UserAuthenticationProvider.php:91, Symfony\Component\Security\Core\Exception\BadCredentialsException(code: 0): The presented password is invalid. at /...../vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Provider/DaoAuthenticationProvider.php:67)"} []</p>
  </blockquote>
  
  <p>thanks, I appreciate your help</p>
  
  <p>ps. my app works pretty well, if I use an <code>in_memory</code> security provider</p>