2

I have some troubles using FOSFacebookBundle with FOSUserBundle for make a subscribtion by Facebook on my website...

There is my code:

base.html.twig:

<html xmlns:fb="http://www.facebook.com/2008/fbml">

<!-- TEMPLATE STUFF -->

function goLogIn(){
    alert('enter goLogin function');
    window.location = "{{ path('_security_check') }}";
}

function onFbInit() {
    alert('enter onFbInit');
    if (typeof(FB) != 'undefined' && FB != null ) {
        FB.Event.subscribe('auth.statusChange', function(response) {
            alert('Status Changed');
            if (response.session || response.authResponse) {
                setTimeout(goLogIn, 500);
            } else {
                window.location = "{{ path('_security_logout') }}";
            }
        });
    }
}
{{ facebook_initialize({'xfbml': true, 'fbAsyncInit': 'onFbInit();'}) }}

config.yml

fos_user:
    db_driver: orm
    firewall_name: main
    user_class: Modnar\Bundle\UserBundle\Entity\User

fos_facebook:
    file:   %kernel.root_dir%/../vendor/facebook/src/base_facebook.php
    alias:  facebook
    app_id: APP_ID                    // I put my own APP_ID here
    secret: APP_SECRET                // I put my own APP_SECRET here
    cookie: true
    permissions: [email, user_birthday, user_location]

services:
    my.facebook.user:
        class: Modnar\Bundle\UserBundle\Security\User\Provider\FacebookProvider
        arguments:
            facebook: "@fos_facebook.api"
            userManager: "@fos_user.user_manager"
            validator: "@validator"
            container: "@service_container"

security.yml

# app/config/security.yml
security:
    factories:
          - "%kernel.root_dir%/../vendor/bundles/FOS/FacebookBundle/Resources/config/security_factories.xml"

    providers:
        fos_userbundle:
            id: fos_user.user_manager
        my_fos_facebook_provider:
            id: my.facebook.user

    encoders:
        FOS\UserBundle\Model\UserInterface: sha512

    firewalls:
        main:
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_provider: form.csrf_provider
            fos_facebook:
                app_url: "http://apps.facebook.com/APP_NAME/" // I put my own APP_NAME here
                server_url: "http://APP_URL/"                 // I put my own APP_URL here
                login_path: /login
                check_path: /login_fb_check
                default_target_path: /
                provider: my_fos_facebook_provider
            logout:
                handlers: ["fos_facebook.logout_handler"]
            anonymous:    true

    access_control:
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/, role: ROLE_ADMIN }

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN

routing.yml

_security_check:
    pattern:  /login_fb_check
_security_logout:
    pattern:  /logout

At this point, the Facebook icon is on my /login page, and I can connect to Facebook, but I'm not redirect anywhere. I put some alert on the js for get a trace, and there is just one alert displayed : "enter onFbInit". Maybe I missed a step somewhere, but I don't find it.

Thanks !

Modnar
  • 21
  • 1
  • what does your FB var containt in your onFbInit function? – Snroki Oct 03 '12 at 13:42
  • FB containt a lot of things. I checked what FB['Event'] containt, that's what I get: subscribers: undefined subscribe: undefined unsubscribe: undefined monitor: undefined clear: undefined fire: undefined listen: undefined unlisten: undefined I print the FB['Event'] content after the FB.Event.subscribe call on base.html.twig. – Modnar Oct 03 '12 at 14:17

1 Answers1

0

I solved that problem by placing the javascript code of facebook directly after opening the the tag.

Look what the documentation says:

Note that we need to include this code before the initialization of the Facebook Javascript SDK Initialization in order to have the onFbInit() event listener correctly triggered (in this case between the beginning of the 'body' tag and the templating helper provided by this bundle)

We wait 500ms before redirecting to let the browser deal with the Facebook cookie. You can avoid this step, but you might get this error message: "The Facebook user could not be retrieved from the session."

Amine Jallouli
  • 3,919
  • 8
  • 36
  • 73