title: Symfony2.2 Login from Database Error: "Bad Credentials"
body: <p>I have roughly followed Symfony's <a href="http://symfony.com/doc/2.2/book/security.html" rel="nofollow">Security</a> and <a href="http://symfony.com/doc/2.2/cookbook/security/entity_provider.html" rel="nofollow">How to load Security Users from the Database</a> instructions with some small custom configurations but I can't login using the users I have in my database! The in_memory user works fine!</p>
  
  <p>This is my security.yml:</p>
  
  <pre><code>jms_security_extra:
      secure_all_services: false
      expressions: true
  
  security:
      encoders:
          NEWS\BlogBundle\Entity\Author: plaintext
  
      role_hierarchy:
          ROLE_ADMIN:       ROLE_USER
  
  
      providers:
          chain_provider:
              chain:
                  providers: [user_db, in_memory]
          user_db:
              entity: { class: NEWSBlogBundle:Author , property: username }
  
          in_memory:
              memory:
                  users:
                      admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }
  
      firewalls:
          admin_area:
                      pattern:    ^/admin
                      http_basic: ~
          secured_area:
                      pattern:  ^/
                      anonymous: ~
                      form_login:
                          login_path: /Login
                          check_path: /login_check
                          username_parameter: username
                          password_parameter: password
  
                      logout:
                          path: /logout
                          target: /blog
  
  
      access_control:
           - { path: ^/admin, roles: ROLE_ADMIN }
           - { path: ^/newpost, roles: ROLE_USER }
  </code></pre>
  
  <p>And my Author.php (User) entity (I have removed the function annotations to make it shorter):</p>
  
  <pre><code>&lt;?php
  
  namespace NEWS\BlogBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  
  /**
   * Author
   */
  class Author implements \Symfony\Component\Security\Core\User\AdvancedUserInterface, \Serializable
  
  {
      /**
       * @var integer
       */
      private $id;
  
      /**
       * @var string
       */
      private $username;
  
      /**
       * @var string
       */
      private $password;
  
      /**
       * @var string
       */
      private $Name;
  
      /**
       * @var string
       */
      private $Surname;
  
      /**
       * @var string
       */
      private $salt;
  
      /**
       * @var boolean
       */
      private $isActive;
  
  
      /**
       * @var \NEWS\BlogBundle\Entity\Category
       */
      private $category;
  
  
  
  
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
  
      public function setUsername($username)
      {
          $this-&gt;username = $username;
  
          return $this;
      }
  
      public function getUsername()
      {
          return $this-&gt;username;
      }
  
      public function setPassword($password)
      {
          $this-&gt;password = $password;
  
          return $this;
      }
  
      public function getPassword()
      {
          return $this-&gt;password;
      }
  
      public function setCategory(\NEWS\BlogBundle\Entity\Category $category = null)
      {
          $this-&gt;category = $category;
  
          return $this;
      }
  
      public function getCategory()
      {
          return $this-&gt;category;
      }
  
      public function setName($name)
      {
          $this-&gt;Name = $name;
  
          return $this;
      }
  
      public function getName()
      {
          return $this-&gt;Name;
      }
  
      public function setSurname($surname)
      {
          $this-&gt;Surname = $surname;
  
          return $this;
      }
  
      public function getSurname()
      {
          return $this-&gt;Surname;
      }
  
      public function __construct()
      {
          $this-&gt;isActive = true;
          $this-&gt; salt = sha1(uniqid(null, true));
      }
  
      public function setSalt($salt)
      {
          $this-&gt;salt = $salt;
  
          return $this;
      }
  
      public function getSalt()
      {
          return $this-&gt;salt;
      }
  
      public function setIsActive($isActive)
      {
          $this-&gt;isActive = $isActive;
  
          return $this;
      }
  
      public function getIsActive()
      {
          return $this-&gt;isActive;
      }
  
      public function isAccountNonExpired()
      {
          // TODO: Implement isAccountNonExpired() method.
          return true;
      }
  
      public function isAccountNonLocked()
      {
          // TODO: Implement isAccountNonLocked() method.
          return true;
      }
  
      public function isCredentialsNonExpired()
      {
          return true;
      }
      public function isEnabled()
      {
              return $this-&gt;isActive;
      }
      public function getRoles()
      {
          return array('ROLE_USER');
      }
  
      public function eraseCredentials()
      {
          // TODO: Implement eraseCredentials() method.
      }
      /**
       * @var \Doctrine\Common\Collections\Collection
       */
      private $posts;
  
  
      public function addPost(\NEWS\BlogBundle\Entity\Post $posts)
      {
          $this-&gt;posts[] = $posts;
  
          return $this;
      }
  
      public function removePost(\NEWS\BlogBundle\Entity\Post $posts)
      {
          $this-&gt;posts-&gt;removeElement($posts);
      }
  
      public function getPosts()
      {
          return $this-&gt;posts;
      }
  
      public function serialize()
      {
          return serialize(array(
              $this-&gt;id,
          ));
      }
  
      public function unserialize($serialized)
      {
          list (
              $this-&gt;id,
              ) = unserialize($serialized);
      }
  }
  </code></pre>
  
  <p>This is my Login.html.twig</p>
  
  <pre><code>{# src/NEWS/BlogBundle/Resources/views/Page/Login.html.twig #}
  {% extends 'NEWSBlogBundle::layout.html.twig' %}
  
  {% block title %}Login Page{% endblock%}
  
  {% block body %}
      &lt;header&gt;
  
      &lt;/header&gt;
  
      {% if app.session.hasFlash('blogger-notice') %}
          &lt;div class="blogger-notice"&gt;
              {{ app.session.flash('blogger-notice') }}
          &lt;/div&gt;
  
      {% endif %}
  
  
      {% if error %}
          &lt;div&gt;{{ error.message }}&lt;/div&gt;
      {% endif %}
  
      &lt;form action="{{ path('login_check') }}" method="post"&gt;
          &lt;label for="username"&gt;Username:&lt;/label&gt;
          &lt;input type="text" id="username" name="username" value="{{ last_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;
  
  {% endblock %}
  </code></pre>
  
  <p>Login controller:</p>
  
  <pre><code>public function LoginAction()
      {
  
          $request = $this-&gt;getRequest();
          $session = $request-&gt;getSession();
  
          // get the login error if there is one
          if ($request-&gt;attributes-&gt;has(SecurityContext::AUTHENTICATION_ERROR)) {
              $error = $request-&gt;attributes-&gt;get(
                  SecurityContext::AUTHENTICATION_ERROR
              );
  
          } else {
              $error = $session-&gt;get(SecurityContext::AUTHENTICATION_ERROR);
              $session-&gt;remove(SecurityContext::AUTHENTICATION_ERROR);
          }
  
          return $this-&gt;render(
              'NEWSBlogBundle:Page:Login.html.twig',
              array(
                  // last username entered by the user
                  'last_username' =&gt; $session-&gt;get(SecurityContext::LAST_USERNAME),
                  'error'         =&gt; $error,
              )
          );
      }
  </code></pre>
  
  <p>and the function which builds my login form:</p>
  
  <pre><code>public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder-&gt;add('username', 'text');
          $builder-&gt;add('password', 'password');
          return $builder-&gt;getForm();
      }
  </code></pre>
  
  <p>I've been trying to solve this for three days but have not been sucessful! I would really appreciate any help!</p>
  
  <p>I don't know if the registration functions are needed too or not, I'm not including them right now since the question is long enough so far!</p>
  
  <p>P.S: My login route intentionally starts with a capital letter and I am aware that I have removed the '_' from the user/pass parameteres, I did try changing them back to the original form but stil got the same error!</p>