Skip to content

Instantly share code, notes, and snippets.

@chalasr
Last active January 11, 2020 11:23
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save chalasr/69ad35c11e38b8cc717f to your computer and use it in GitHub Desktop.
Save chalasr/69ad35c11e38b8cc717f to your computer and use it in GitHub Desktop.
FOSUserBundle Login Authentication Success Handler example.
<?php
namespace AppBundle\EventListener;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
class AuthenticationSuccessHandler implements AuthenticationSuccessHandlerInterface
{
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
$token = $event->getAuthenticationToken();
$request = $event->getRequest();
$this->onAuthenticationSuccess($request, $token);
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
return new RedirectResponse($referer);
}
}
security:
# ...
firewalls:
# ...
user:
pattern: /(.*)
form_login:
provider: fos_userbundle
login_path: /login
use_forward: false
check_path: /login_check
failure_path: null
success_handler: app.authentication_success_handler
logout:
path: /logout
services:
# ...
app.authentication_success_handler:
class: AppBundle\EventListener\AuthenticationSuccessHandler
tags:
- { name: kernel.event_listener, event: security.interactive_login, method: onSecurityInteractiveLogin }
@chalasr
Copy link
Author

chalasr commented Apr 1, 2016

Fully working example on Symfony 2.8.4 with FOSUserBundle 1.3.6.

@likai24
Copy link

likai24 commented Dec 15, 2016

The security.context service was deprecated in the 2.6 and split into two new services: security.authorization_checker and security.token_storage.
see this http://stackoverflow.com/questions/36531853/you-have-requested-a-non-existent-service-security-context

@e-vural
Copy link

e-vural commented Jan 5, 2018

Where is $referer variable ? Need $request->headers->get("referer"); ?

@aliemre
Copy link

aliemre commented Jan 23, 2018

@emrevural20 Yes $request->headers->get("referer"); works!

@Jules59
Copy link

Jules59 commented Jul 24, 2018

coj

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment