title: FOSFacebookBundle and FOSUserBundle
body: <p>I'm trying to setup FOSFacebookBundle and FOSUserBundle, so users can login with registered accounts or facebook accounts.</p>
  
  <p>Here relevant codes:</p>
  
  <p>config.yml:</p>
  
  <pre><code>fos_user:
  db_driver: orm 
  firewall_name: public
  user_class: Fam\DiaBundle\Entity\User
  from_email:
      address:        info@famdia.com
      sender_name:    Staff
  registration:
      confirmation:
          enabled:    true
  
  fos_facebook:
  
      file:   %kernel.root_dir%/../vendor/facebook/src/base_facebook.php
      alias:  facebook
      app_id: 1234567890
      secret: abcdefg1234567890
      cookie: true
      permissions: [email]
      culture: us_US
  </code></pre>
  
  <p>security.yml</p>
  
  <pre><code>#
  # app/config/security.yml
  #
  #
  
  services:
      my.facebook.user:
          class: Fam\Dia\Security\User\Provider\FacebookProvider
          arguments:
              facebook: "@fos_facebook.api"
              userManager: "@fos_user.user_manager"
              validator: "@validator"
              container: "@service_container"
  security:
      factories:
          - "%kernel.root_dir%/../vendor/bundles/FOS/FacebookBundle/Resources/config/security_factories.xml"
  
      providers:
          chain_provider:
              providers: [fos_userbundle, my_fos_facebook_provider]
          fos_userbundle:
              id: fos_user.user_manager
          my_fos_facebook_provider:
              id: my.facebook.user
  
  encoders:
          "FOS\UserBundle\Model\UserInterface": sha512
  
      firewalls:
          public:
            pattern:   ^/
            fos_facebook:
              app_url: "http://apps.facebook.com/dia/"
              server_url: "http://diafam.com/facebookApp/"
              login_path: /login
              check_path: /login_check
              default_target_path: /
              provider: my_fos_facebook_provider
            form_login:
              login_path: /login
              check_path: /login_check
              provider: fos_userbundle
            anonymous: true
            logout:    true            
  
  
      access_control:
          - { path: ^/secured/.*, role: [IS_AUTHENTICATED_FULLY] } # This is the route secured with     fos_facebook
          - { path: ^/facebook/,   role: [ROLE_FACEBOOK] }
          - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
          - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
          - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
          - { path: ^/admin/, role: ROLE_ADMIN }
          # FOS facebook
          - { path: ^/login_check, role: IS_AUTHENTICATED_ANONYMOUSLY }
          - { path: ^/.*, role: [IS_AUTHENTICATED_ANONYMOUSLY] }
          #- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
  </code></pre>
  
  <p>routing.yml</p>
  
  <pre><code>_security_check:
        pattern:  /login_check
  _security_logout:
        pattern:  /logout
  
  _security_login:
        pattern:   /login
  </code></pre>
  
  <p>FacebookProvider.php</p>
  
  <pre><code>&lt;?php
  
  // src/Fam\DiaBundle\Security\User\Provider/FacebookProvider.php
  
  namespace Fam\DiaBundle\Security\User\Provider;
  
  use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  use Symfony\Component\Security\Core\User\UserProviderInterface;
  use Symfony\Component\Security\Core\User\UserInterface;
  use \BaseFacebook;
  use \FacebookApiException;
  
  class FacebookProvider implements UserProviderInterface
  {
      /**
       * @var \Facebook
       */
      protected $facebook;
  protected $userManager;
  protected $validator;
  
  public function __construct(BaseFacebook $facebook, $userManager, $validator)
  {
      $this-&gt;facebook = $facebook;
      $this-&gt;userManager = $userManager;
      $this-&gt;validator = $validator;
  }
  
  public function supportsClass($class)
  {
      return $this-&gt;userManager-&gt;supportsClass($class);
  }
  
  public function findUserByFbId($fbId)
  {
      return $this-&gt;userManager-&gt;findUserBy(array('facebookId' =&gt; $fbId));
  }
  
  public function loadUserByUsername($username)
  {
      $user = $this-&gt;findUserByFbId($username);
  
      try {
          $fbdata = $this-&gt;facebook-&gt;api('/me');
      } catch (FacebookApiException $e) {
          $fbdata = null;
      }
  
      if (!empty($fbdata)) {
          if (empty($user)) {
              $user = $this-&gt;userManager-&gt;createUser();
              $user-&gt;setEnabled(true);
              $user-&gt;setPassword('');
          }
  
          // TODO use http://developers.facebook.com/docs/api/realtime
          $user-&gt;setFBData($fbdata);
  
          if (count($this-&gt;validator-&gt;validate($user, 'Facebook'))) {
              // TODO: the user was found obviously, but doesnt match our expectations, do something smart
              throw new UsernameNotFoundException('The facebook user could not be stored');
          }
          $this-&gt;userManager-&gt;updateUser($user);
      }
  
      if (empty($user)) {
          throw new UsernameNotFoundException('The user is not authenticated on facebook');
      }
  
      return $user;
  }
  
  public function refreshUser(UserInterface $user)
  {
      if (!$this-&gt;supportsClass(get_class($user)) || !$user-&gt;getFacebookId()) {
          throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
      }
  
      return $this-&gt;loadUserByUsername($user-&gt;getFacebookId());
      }
  }
  </code></pre>
  
  <p>login.html.twig</p>
  
  <pre><code>{% extends '::base.html.twig' %}
  
  
  {% block body %}
  {{ facebook_initialize({'xfbml': true, 'fbAsyncInit': 'onFbInit();'}) }}
  {{ facebook_login_button({'autologoutlink': true}) }}
  &lt;script&gt;
    function goLogIn(){
        window.location.href = "{{ path('_security_check') }}";
    }
  
    function onFbInit() {
        if (typeof(FB) != 'undefined' &amp;&amp; FB != null ) {
            FB.Event.subscribe('auth.statusChange', function(response) {
                if (response.session || response.authResponse) {
                    setTimeout(goLogIn, 500);
                } else {
                    window.location.href = "{{ path('_security_logout') }}";
                }
            });
        }
    }
  &lt;/script&gt;
  
  
      {% for key, message in app.session.getFlashes() %}
      &lt;div class="{{ key }}"&gt;
          {{ message|trans({}, 'FOSUserBundle') }}
      &lt;/div&gt;
      {% endfor %}
  
  
  {% if error %}
      &lt;div&gt;{{ error }}&lt;/div&gt;
  {% endif %}
  
  &lt;form action="{{ path("fos_user_security_check") }}" method="post"&gt;
      &lt;h5&gt;&lt;label for="username"&gt;{{ 'security.login.username'|trans({}, 'FOSUserBundle') }}&lt;/label&gt;&lt;br/&gt;
      &lt;input type="text" id="username" name="_username" value="{{ last_username }}" size=30 /&gt;&lt;p&gt;
  
      &lt;label for="password"&gt;{{ 'security.login.password'|trans({}, 'FOSUserBundle') }}&lt;/label&gt;&lt;br/&gt;
      &lt;input type="password" id="password" name="_password" size=30 /&gt;&lt;p&gt;
  
      &lt;input type="checkbox" id="remember_me" name="_remember_me" value="on" /&gt;
      &lt;label for="remember_me"&gt;{{ 'security.login.remember_me'|trans({}, 'FOSUserBundle') }}&lt;/label&gt;&lt;p&gt;
  
      &lt;input type="submit" id="_submit" name="_submit" value="Entra" /&gt;
  &lt;/form&gt;
          &lt;/div&gt;
  
  {% endblock body %}
  </code></pre>
  
  <p>May you help me?</p>