title: Symfony3 Guard and login form
body: <p>I try to use Guard to make a login form instead of the security.yml way.</p>
  
  <p>Getuser and checkcredential are ok.<br>
  onAuthenticationSuccess is ok (if I put a <code>dump($token); die;</code> in onAuthenticationSuccess I can see my user in the token) and redirect to /accueil.<br>
  But when it arrived on /accueil it's send back to /login because user authentication is always on anon !</p>
  
  <p>Impossible to find a solution :c/</p>
  
  <p>Firewall in security.yml :</p>
  
  <pre><code>  firewalls:
       dev:
          pattern: ^/(_(profiler|wdt)|css|images|js)/
          security: false
  
      login_firewall:
          pattern:   ^/login$
          anonymous: true
  
      main:
          pattern:    ^/
          anonymous: ~
          logout: ~
          switch_user: true
          guard:
               provider: database
               authenticators:
                 - ent.login_authenticator
  
  access_control:
    - { path: ^/login$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin/, roles: ROLE_ADMIN }
    - { path: ^/, roles: ROLE_USER }
  </code></pre>
  
  <p>securityController</p>
  
  <pre><code>/**
   * @Route("/login", name="login")
   *
   */
  public function loginAction(Request $request)
  {
  
    if ($this-&gt;get('security.authorization_checker')-&gt;isGranted('IS_AUTHENTICATED_REMEMBERED')) {
      return $this-&gt;redirectToRoute('accueil');
    }
  
    $authenticationUtils = $this-&gt;get('security.authentication_utils');
  
    $exception = $authenticationUtils-&gt;getLastAuthenticationError();
  
    $lastUsername = $authenticationUtils-&gt;getLastUsername();
  
    return $this-&gt;render('EntBundle::login.html.twig', [
      'last_username' =&gt; $lastUsername,
      'error' =&gt; $exception,
    ]);
  
  }
  
  /**
   * @Route("/login_check", name="login_check")
   */
  public function loginCheckAction()
  {
      // this controller will not be executed,
      // as the route is handled by the Security system
  }
  </code></pre>
  
  <p>loginAuthenticator:</p>
  
  <pre><code>  public function __construct(RouterInterface $router, UserPasswordEncoder   $passwordEncoder, EntityManager $em) {
  $this-&gt;router = $router;
  $this-&gt;passwordEncoder = $passwordEncoder;
    $this-&gt;em = $em;
   }
  
  public function getCredentials(Request $request)
  {
    if ($request-&gt;getPathInfo() != '/login_check' ) {
        return null;
    }
  
    $request-&gt;getSession()-&gt;set(Security::LAST_USERNAME, $request-&gt;request-&gt;get('_username'));
  
    return array(
        'username' =&gt; $request-&gt;request-&gt;get('_username'),
        'password' =&gt; $request-&gt;request-&gt;get('_password'),
    );
  }
  
  public function getUser($credentials, UserProviderInterface $userProvider)
  {
    try {
        return $this-&gt;em-&gt;getRepository('EntBundle:User\User')-&gt;findOneBy(array('username' =&gt; $credentials ));
    }
    catch (UsernameNotFoundException $e) {
        throw new CustomUserMessageAuthenticationException($this-&gt;failMessage);
    }
   }
  
   public function checkCredentials($credentials, UserInterface $user) {
  
    $plainPassword = $credentials['password'];
    if ($this-&gt;passwordEncoder-&gt;isPasswordValid($user, $plainPassword)) {
        return true;
    }
  
    throw new CustomUserMessageAuthenticationException($this-&gt;failMessage);
   }
  
   public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
   {
     //      dump($token); die;
    $url = $this-&gt;router-&gt;generate('accueil');
    return new RedirectResponse($url);
   }
  
   public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
   {
    $request-&gt;getSession()-&gt;set(Security::AUTHENTICATION_ERROR, $exception);
  
    $url = $this-&gt;router-&gt;generate('login');
    return new RedirectResponse($url);
   }
  
   public function start(Request $request, AuthenticationException $authException = null)
   {
    $url = $this-&gt;router-&gt;generate('login');
    return new RedirectResponse($url);
   }
  </code></pre>