title: Symfony3 Invalid credentials, UsernameNotFoundException(code: 0): Username \"\" does not exist
body: <p>Login tutorial: <a href="http://symfony.com/doc/current/security/form_login_setup.html" rel="nofollow noreferrer">http://symfony.com/doc/current/security/form_login_setup.html</a></p>
  
  <p>I'm getting an error when trying to log in: Invalid credentials.</p>
  
  <p>Here's dev.log:</p>
  
  <pre><code>[2017-01-16 22:57:51] request.INFO: Matched route "login". {"route":"login","route_parameters":{"_controller":"AppBundle\\Controller\\SecurityController::loginAction","_route":"login"},"request_uri":"http://localhost:8000/login","method":"GET"} []
  [2017-01-16 22:57:51] security.INFO: Populated the TokenStorage with an anonymous Token. [] [][2017-01-16 23:00:46] request.INFO: Matched route "login". {"route":"login","route_parameters":{"_controller":"AppBundle\\Controller\\SecurityController::loginAction","_route":"login"},"request_uri":"http://localhost:8000/login","method":"POST"} []
  [2017-01-16 23:00:46] security.INFO: Authentication request failed. {"exception":"[object] (Symfony\\Component\\Security\\Core\\Exception\\BadCredentialsException(code: 0): Bad credentials. at /Users/mac/symfony-project/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Provider/UserAuthenticationProvider.php:73, Symfony\\Component\\Security\\Core\\Exception\\UsernameNotFoundException(code: 0): Username \"admin\" does not exist. at /Users/mac/symfony-project/vendor/symfony/symfony/src/Symfony/Component/Security/Core/User/InMemoryUserProvider.php:109)"} []
  </code></pre>
  
  <p>dump($authenticationUtils->getLastAuthenticationError()):</p>
  
  <pre><code>SecurityController.php on line 28:
  BadCredentialsException {#100 ▼
    -token: UsernamePasswordToken {#99 ▼
      -credentials: "abcdef"
      -providerKey: "main"
      -user: "admin"
      -roles: []
      -authenticated: false
      -attributes: []
    }
    #message: "Bad credentials."
    #code: 0
    #file:     "../vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Provider/UserAuthentication ▶"
    #line: 73
  </code></pre>
  
  <p>Security.yml</p>
  
  <pre><code>security:
    encoders:
        AppBundle\Entity\User:
            algorithm: bcrypt
            cost: 12
  
    providers:
        in_memory:
            memory: ~
        user_provider:
            entity:
                class: AppBundle\Entity\User
                property: username
  
    firewalls:
        main:
            anonymous: ~
            form_login:
                login_path: login
                check_path: login
  </code></pre>
  
  <p>SecurityController:</p>
  
  <pre><code>/**
   * @Route("/login", name="login")
   */
  public function loginAction(Request $request)
  {
      $form = $this-&gt;createForm(LoginType::class);
  
      $authenticationUtils = $this-&gt;get('security.authentication_utils');
  
      $a = 1;
      $a = 0;
  
      if ($a == 1)
          $error = $authenticationUtils-&gt;getLastAuthenticationError();
      else
          die(dump($authenticationUtils-&gt;getLastAuthenticationError()));
  
      $lastUsername = $authenticationUtils-&gt;getLastUsername();
  
      return $this-&gt;render('security/login.html.twig', array(
          'form' =&gt; $form-&gt;createView(),
          'last_username' =&gt; $lastUsername,
          'error' =&gt; $error,
      ));
  }
  </code></pre>
  
  <p>Login form:</p>
  
  <pre><code>&lt;form action="{{ path('login') }}" method="post"&gt;
              &lt;label for="username"&gt;Username:&lt;/label&gt;
              &lt;input type="text" id="username" name="_username" /&gt;
  
              &lt;label for="password"&gt;Password:&lt;/label&gt;
              &lt;input type="password" id="password" name="_password" /&gt;
  
              &lt;button type="submit"&gt;login&lt;/button&gt;
          &lt;/form&gt;
  </code></pre>
  
  <p>User Entity:</p>
  
  <pre><code>&lt;?php
  
  namespace AppBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\Security\Core\User\UserInterface;
  use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  use Symfony\Component\Validator\Constraints as Assert;
  
  /**
   * User
   *
   * @ORM\Table(name="user")
   * @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
   * @UniqueEntity(fields="username", message="Username already taken")
   */
  class User implements UserInterface
  {
      /**
       * @var int
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var string
       * @Assert\NotBlank()
       * @ORM\Column(name="username", type="string", length=255, unique=true)
       */
      private $username;
  
      /**
       * @var string
       *
       * @ORM\Column(name="password", type="string", length=255)
       */
      private $password;
  
      /**
       * @var \DateTime
       *
       * @ORM\Column(name="created_at", type="datetime")
       */
      private $createdAt;
  
      /**
       * @Assert\NotBlank()
       * @Assert\Length(max=255)
       */
      private $plainPassword;
  
      public function __construct()
      {
          $this-&gt;createdAt = new \DateTime();
      }
  
      /**
       * Get id
       *
       * @return int
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set username
       *
       * @param string $username
       *
       * @return User
       */
      public function setUsername($username)
      {
          $this-&gt;username = $username;
  
          return $this;
      }
  
      /**
       * Get username
       *
       * @return string
       */
      public function getUsername()
      {
          return $this-&gt;username;
      }
  
      /**
       * Set password
       *
       * @param string $password
       *
       * @return User
       */
      public function setPassword($password)
      {
          $this-&gt;password = $password;
  
          return $this;
      }
  
      /**
       * Get password
       *
       * @return string
       */
      public function getPassword()
      {
          return $this-&gt;password;
      }
  
      /**
       * Set createdAt
       *
       * @param \DateTime $createdAt
       *
       * @return User
       */
      public function setCreatedAt($createdAt)
      {
          $this-&gt;createdAt = $createdAt;
  
          return $this;
      }
  
      /**
       * Get createdAt
       *
       * @return \DateTime
       */
      public function getCreatedAt()
      {
          return $this-&gt;createdAt;
      }
  
      /**
       * Get plain password
       *
       * @return string
       */
      public function getPlainPassword()
      {
          return $this-&gt;plainPassword;
      }
  
      /**
       * Set plain password
       *
       * @param string $password
       *
       * @return Users
       */
      public function setPlainPassword($password)
      {
          $this-&gt;plainPassword = $password;
  
          return $this;
      }
  
      public function getRoles()
      {
          return array('ROLE_USER');
      }
  
      public function eraseCredentials()
      {
      }
  
      public function getSalt()
      {
      }
  }
  </code></pre>
  
  <p>Where could be the problem?</p>