34

For login success there is a parameter use_referer: true. For login failure there is only failure_path, which isn't what I'm looking for.

Second thing: How to do that and pass error message?

Third thing: How to go back to referrer after logout?

Falko
  • 17,076
  • 13
  • 60
  • 105
Wojciech Kulik
  • 7,823
  • 6
  • 41
  • 67
  • why wouldn't the failure path just be the login prompt? – JamesHalsall Jan 30 '12 at 13:19
  • 2
    because I can login from every subpage. I haven't got one login form on specified url, so I don't want to redirect user to homepage when he's login from for example another user profile page. – Wojciech Kulik Jan 30 '12 at 13:30

1 Answers1

73

I solved it.

There is solution: How to disable redirection after login_check in Symfony 2

and here is code which solves my problem:

<?php

namespace Acme\MainBundle\Handler;

use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;

class AuthenticationHandler implements AuthenticationFailureHandlerInterface, LogoutSuccessHandlerInterface
{
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
    {       
        $referer = $request->headers->get('referer');       
        $request->getSession()->setFlash('error', $exception->getMessage());

        return new RedirectResponse($referer);
    }

    public function onLogoutSuccess(Request $request) 
    {
        $referer = $request->headers->get('referer');
        return new RedirectResponse($referer);
    }
}

to handle events add to security.yml for example:

form_login:
    check_path: /access/login_check
    login_path: /
    use_referer: true
    failure_handler: authentication_handler  
logout:
    path:   /access/logout
    target: /
    success_handler: authentication_handler 

and to config.yml:

services:
    authentication_handler:
        class: Acme\MainBundle\Handler\AuthenticationHandler
Community
  • 1
  • 1
Wojciech Kulik
  • 7,823
  • 6
  • 41
  • 67
  • 5
    $request->headers->get('referer'); sometimes returns null (I've reproduced in Firefox). This solution is not reliable, a fallback or another solution is necessary. – Julien Feb 18 '13 at 10:43
  • @wojciech-kulik I have the following error: ```$request->getSession()->setFlash('error', $exception->getMessage());``` ```FatalErrorException: Error: Call to undefined method Symfony\Component\HttpFoundation\Session\Session::setFlash() in /var/www/symfony/src/Application/Sonata/UserBundle/Form/Handler/AuthenticationHandler.php line 16``` – jcarlosweb Feb 26 '14 at 09:32
  • @webyseo Probably you've got a different version of Symphony and flash messages are set in a different way. Maybe it will help: http://stackoverflow.com/questions/13348534/symfony-2-setting-a-flash-message-outside-of-controller – Wojciech Kulik Feb 26 '14 at 18:52
  • 2
    @WojciechKulik Exactly solved with ```$session->getFlashBag()->add('error', $exception->getMessage());``` thanks!! – jcarlosweb Feb 26 '14 at 19:10
  • How to get doctrine in onAuthenticationFailure function for login throttling. $user->setLocked(true); – Ramesh Jul 01 '16 at 12:10
  • how to use doctrine inside onAuthenticationFailure function.? – Ramesh Jul 25 '16 at 10:00
  • @Ramesh ask this in your own question ;) – Veve Aug 16 '16 at 09:38