Symfony2 : redirect to referer after login successful (FOSUserBundle)
<p>My redirect works for logout but not for login, it stays on the login page BUT referer has the proper value (I displayed <code>{{ app.request.headers.get('referer') }}</code> in my login form).</p>

<p><strong>Security.yml</strong>:</p>

<pre><code>firewalls:
   main:
       pattern: ^/
       form_login:
           provider:        fos_userbundle
           csrf_provider:   security.csrf.token_manager
           use_referer:     true
           success_handler: authentication_handler
       logout:
           success_handler: authentication_handler
       anonymous: true
</code></pre>

<p><strong>Handler</strong>:</p>

<pre><code>namespace LeJardinEbene\Bundle\Form\Handler;

use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;

class AuthenticationHandler implements AuthenticationSuccessHandlerInterface, LogoutSuccessHandlerInterface
{
    public function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {
        $referer = $request-&gt;headers-&gt;get('referer');
        return new RedirectResponse($referer);
    }

    public function onLogoutSuccess(Request $request)
    {
        $referer = $request-&gt;headers-&gt;get('referer');
        return new RedirectResponse($referer);
    }
}
</code></pre>

<p><strong>Services.yml</strong>:</p>

<pre><code>services:
    authentication_handler:
        class: LeJardinEbene\Bundle\Form\Handler\AuthenticationHandler
</code></pre>

<p>Does someone know what the issue is?</p>

<p><strong>UPDATE</strong></p>

<p>As you advised me I splitted the handler in 2 :</p>

<pre><code>class LoginAuthenticationHandler
{
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
    //$referer = $request-&gt;headers-&gt;get('referer');
    $referer = $event-&gt;getRequest()-&gt;headers-&gt;get('referer');
    return new RedirectResponse($referer);
}
}


class LogoutAuthenticationHandler implements LogoutSuccessHandlerInterface
{
public function onLogoutSuccess(Request $request)
{
    $referer = $request-&gt;headers-&gt;get('referer');
    return new RedirectResponse($referer);
}
}


services:
login_authentication_handler:
    class: LeJardinEbene\Bundle\Form\Handler\LoginAuthenticationHandler
    tags:
        - { name: kernel.event_listener, event: security.interactive_login, method: onSecurityInteractiveLogin }

logout_authentication_handler:
    class: LeJardinEbene\Bundle\Form\Handler\LogoutAuthenticationHandler
</code></pre>

<p>But now I get the following error :</p>

<pre><code>Fatal error: Uncaught exception 'Symfony\Component\Debug\Exception\ContextErrorException' with message 'Catchable Fatal Error: Argument 1 passed to Symfony\Component\Security\Http\Authentication\CustomAuthenticationSuccessHandler::__construct() must implement interface Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface, instance of LeJardinEbene\Bundle\Form\Handler\LoginAuthenticationHandler given
</code></pre>

<p><strong>UPDATE 2</strong></p>

<pre><code>namespace LeJardinEbene\Bundle\Form\Handler;

use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;

class LoginAuthenticationHandler extends AuthenticationSuccessHandlerInterface
{
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
    $referer = $event-&gt;getRequest()-&gt;headers-&gt;get('referer');
    return new RedirectResponse($referer);
}
}
</code></pre>

<p>Produces the error :</p>

<pre><code>Fatal error: Class LeJardinEbene\Bundle\Form\Handler\LoginAuthenticationHandler cannot extend from interface Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface in /Applications/XAMPP/xamppfiles/htdocs/symfony2/src/LeJardinEbene/Bundle/Form/Handler/LoginAuthenticationHandler.php on line 22
</code></pre>

<p><strong>UPDATE 3</strong></p>

<p>I already tried and it causes this error : </p>

<pre><code>Fatal error: Class LeJardinEbene\Bundle\Form\Handler\LoginAuthenticationHandler contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface::onAuthenticationSuccess) in /Applications/XAMPP/xamppfiles/htdocs/symfony2/src/LeJardinEbene/Bundle/Form/Handler/LoginAuthenticationHandler.php on line 22
</code></pre>

<p>and if I do this everything has to changed no ? I mean, no more $event etc. but $request and $token again as arguments ... which didn't work when I opened this topic, arghhhhhhhhhhh !!</p>

<p><strong>UPDATE 4</strong></p>

<p>services.yml</p>

<pre><code>services:
login_handler:
    class: LeJardinEbene\Bundle\Form\Handler\LoginAuthenticationHandler
    tags:
        - { name: kernel.event_listener, event: security.interactive_login, method: onSecurityInteractiveLogin }
</code></pre>

<p>security.yml</p>

<pre><code>firewalls:
   main:
       pattern: ^/
       form_login:
           provider:        fos_userbundle
           csrf_provider:   security.csrf.token_manager
           use_referer:     true
           success_handler: login_handler
       logout:
           success_handler: logout_authentication_handler
       anonymous: true
</code></pre>