1

In my symfony2 project I'm using the FOSFacebookBundle and it's working ok. If the fb user already exists on the DB the user is logged in, by another hand if the user is not registered yet, the user will be added to the DB, and the data retrieved from FB.

So far so good, but now I want to redirect the user to the profile edit page if the user was registered in this process, otherwise will be redirect to the homepage. How can I implement this? I just know if the user exists or not in the provider, should I register some information to the session in the provider and then create some listener to read the session and do the redirection?

Any hints are welcome :)

Zeux
  • 372
  • 1
  • 4
  • 12

2 Answers2

1

You need to configure a custom authentication success handler and authentication failurehandler. Configure a service that implements AuthenticationSuccessHandlerInterface: and AuthenticationFailureHandlerInterface

facebook_auth_success_handler:
         class: MyHandler
         public: false
         arguments:
             # your dependencies...

Then add this handler to security.yml under your fos_facebook block:

firewalls:
foo:
    fos_facebook:
        success_handler: facebook_auth_success_handler
Nisam
  • 2,275
  • 21
  • 32
0

As you are using the FOSUserBundle (as per the tags on your question), you can hook up into the controllers and redirect on the event REGISTRATION_SUCCESS (when a user is created).

In your case, it should go as follow:

// src/Acme/UserBundle/EventListener/RegistrationSuccessListener.php
class RegistrationSuccessListener implements EventSubscriberInterface
{
    private $router;

    public function __construct(UrlGeneratorInterface $router)
    {
        $this->router = $router;
    }

    public static function getSubscribedEvents()
    {
        return array(
                FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess'
        );
    }

    public function onRegistrationSuccess(GetResponseUserEvent $event)
    {
        $url = $this->router->generate('profile_edit_page');

        $event->setResponse(new RedirectResponse($url));
    }
}

And your service.yml:

services:
    acme_user.registration.success:
        class: Acme\UserBundle\EventListener\RegistrationSuccessListener
        arguments: [@router]
        tags:
            - { name: kernel.event_subscriber }

https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/controller_events.md

cheesemacfly
  • 11,622
  • 11
  • 53
  • 72
  • 1
    Thanks for the answer but this doesn't solve my problem. Since I'm using the fosfacebookbundle together with fosuserbundle, the registration with facebook is not considered a registration but just a simple login, so the event FOSUserEvents::REGISTRATION_SUCCESS is not dispatched. By the way, your code works for a simple registration, you should just replace `onRegistrationSuccess(GetResponseUserEvent $event)` with `onRegistrationSuccess(FormEvent $event)` – Zeux May 28 '13 at 09:26
  • Even in the second case when the user is added to the DB? You don't have access to any of the available events from the FOSUserBundle? – cheesemacfly May 28 '13 at 15:13