title: Symfony 2.3 custom form login_check
body: <p>I have successfully created a login system that checks a doctrine user entity class and authenticates successfully. This uses http_basic which is set in the firewalls block in:</p>
  
  <p>app/config/security.yml</p>
  
  <p>However I wish to use the form_login option and render my own custom login template. Below is the app/config/security.yml after i made changes to use form login which renders my custom form:</p>
  
  <pre><code>security:
  encoders:
      Brs\UserBundle\Entity\User:
          algorithm: bcrypt
          cost: 12
  
  role_hierarchy:
      ROLE_ADMIN:       ROLE_USER
      ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
  
  providers:
      # in_memory:
      #     memory:
      #         users:
      #             user:  { password: userpass, roles: [ 'ROLE_USER' ] }
      #             admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }
      administrators:
          entity: { class: BrsUserBundle:User, property: fname }
  
  firewalls:
      login: 
          pattern: ^/admin/login$
          anonymous: ~
      admin_area:
          pattern: ^/admin/
          # http_basic: ~
          form_login:
              login_path: /admin/login
              check_path: /admin/login_check
  
  access_control:
      - { path: ^/admin/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
      - { path: ^/admin, roles: ROLE_ADMIN }
  </code></pre>
  
  <p>Now my form template posts to the check_path defined under form_login in security.yml.</p>
  
  <p>I have the 2 routes defined within the bundle in LoginBundle/config/routing.yml</p>
  
  <pre><code>admin:
      path: /admin/login
      defaults: { _controller: BrsLoginBundle:Login:index }
  
  adminlogin:
      path: /admin/login_check
      defaults: { _controller: BrsLoginBundle:Login:loginCheck }
  
  admin_hello:
      path: /admin/hello
      defaults: { _controller: BrsLoginBundle:Login:hello }
  </code></pre>
  
  <p>When i submit the form it will return me to the /admin/login/ url and i cannot seem to get this to authenticate.</p>
  
  <p>From my understanding in the LoginController.php the symfony 2 docs tell me to define the method loginCheckAction(), which is empty and returns nothing.</p>
  
  <p>If i am following this correctly the form posts to the login_check path and the security system should handle the authentication just like when I used http_basic.</p>
  
  <p>I am baffled and I am currently reading the docs again to see if i have missed something silly.</p>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p>Adam</p>
  
  <p>EDIT*</p>
  
  <p>Here is the twig template that renders the form:</p>
  
  <pre><code>    {% extends 'BrsLoginBundle::layout.html.twig' %}
  
  {% block body %}
         &lt;h1&gt;{{ name }}&lt;/h1&gt;
  {% if error %}
      &lt;div&gt;{{ error.messageKey|trans(error.messageData) }}&lt;/div&gt;
  {% endif %}       {#{form_start(form,{ 'attr': {'novalidate': 'novalidate' }})}#}
         &lt;form action="{{ path('login_check')}}" method="post"&gt;
         &lt;input type="text" name="_username" /&gt;
         &lt;input type="password" name="_password" /&gt;
         &lt;button type="submit"&gt;Login&lt;/button&gt;
         &lt;/form&gt;
         {#{form_widget(form)}#}
         {#{form_end(form)}#}
  {% endblock %}
  </code></pre>
  
  <p>here is the controller</p>
  
  <pre><code>&lt;?php
      namespace Brs\LoginBundle\Controller;
  
      use Symfony\Bundle\FrameworkBundle\Controller\Controller;
      use Symfony\Component\HttpFoundation\Request;
      use Symfony\Component\HttpFoundation\Response;
      use Brs\LoginBundle\Form\Type\LoginType;
      use Symfony\Component\Security\Core\SecurityContextInterface;
  
  
      class LoginController extends Controller{
  
          public function indexAction(Request $request){
              $session = $request-&gt;getSession();
              //instantiate the form type to use
              //$login = new LoginType();
              // create the form based on the type above
              //$form = $this-&gt;createForm(new LoginType(), $login);
              // this will tell me wether the form was submitted or not and handle the validation defined in bundle/resources/config/validation.yml
              //$form-&gt;handleRequest($request);
              //checks if the form is valid then we will execute what we want to next
              //if($form-&gt;isValid()){
                  //do something if all validation test are passed        
              //} 
              var_dump($request-&gt;attributes-&gt;has(SecurityContextInterface::AUTHENTICATION_ERROR));
              var_dump($session-&gt;has(SecurityContextInterface::AUTHENTICATION_ERROR));
              var_dump(null !== $session);
  
              $error = $session-&gt;get(SecurityContextInterface::AUTHENTICATION_ERROR);
              $session-&gt;remove(SecurityContextInterface::AUTHENTICATION_ERROR);   
              //print_r($request-&gt;attributes-&gt;get(SecurityContextInterface::AUTHENTICATION_ERROR));
              //print_r($session-&gt;get(SecurityContextInterface::AUTHENTICATION_ERROR));
              //render the login template and pass the created form to it
              return $this-&gt;render('BrsLoginBundle:Admin:login.html.twig',array(
                  'name'=&gt;"Login",
                  'error'=&gt;$error
                  //'form'=&gt;$form-&gt;createView(),
              ));
          }
  
          public function hello(){
              return new Response("i shoudlnt be able to see this");
          }
  
          public function loginCheckAction(){
  
          }
      }
  ?&gt;  
  </code></pre>
  
  <p>Here is the link to the documentation that I have been following:</p>
  
  <p><a href="http://symfony.com/doc/2.3/cookbook/security/form_login_setup.html" rel="nofollow">http://symfony.com/doc/2.3/cookbook/security/form_login_setup.html</a></p>
  
  <p>EDIT*</p>
  
  <p>I have tailed the logs and this is Authenticating the user, However when it calls the login_check method , it then attempts to reload the user from the database but fails to find one. so it starts the login procedure over again. The logs are in the link below:</p>
  
  <p><a href="http://pastebin.com/sfTCNJS7" rel="nofollow">http://pastebin.com/sfTCNJS7</a></p>