sfFbConnectGuardPlugin - 0.0.1

Setfive Consulting contact@setfive.com

You are currently browsing
the website for symfony 1

Visit the Symfony2 website

« Back to the Plugins Home


Forgot your password?
Create an account



advanced search
Information Readme Releases Changelog Contribute
Show source


The sfFbConnectGuardPlugin adds a route, "fb_connect_auth", to allow Facebook Connect users to be created and logged in using the sfGuard plugin.

The basic flow of events is as follows:

  1. A user requests to sign up via FB Connect on your site.

  2. Your site initiates a FB Connect request via Javascript (explained in detail later).

  3. The user "connects" and Facebook forwards them to fb_connect_auth route.

  4. sfFbConnectGuardPlugin receives the newly FB Connected user, creates them a sfGuard user, optionally logs them in, and forwards them exactly like sfGuard.

The flow is nearly identical when a user is signing in via FB Connect EXCEPT that it is your responsibility to forward them to the fb_connect_auth in order for the plugin to recieve them and login their sfGuard user.


  • Create a Facebook Application - details at http://developers.facebook.com/get_started.php

    • Important: Make a note of your Facebook API key and Facebook secret key
    • Important: When configuring the application, on the "Connect" tab you need to set "Connect URL" to the URL where the "fb_connect_auth" route points. For example, http://www.setfive.com/fb/auth
  • Install the plugin

        $ symfony plugin:install sfFbConnectGuardPlugin
  • Configure the plugin in app.yml

      facebook_api_key: somekey # your Facebook application API key (get it from Facebook)
      facebook_secret: somesecret # your Facebook application secret key (get it from Facebook)
        auto_login: false # if users should be automatically logged in after they sign up with FB Connect
        connect_successful: register_please_confirm # where users go after they succesfully connect
        login_failed: someroute # where to go if the login failed
        # where to go when a user successfully logs in versus registers with FB Connect
        success_signin_url: someroute
        # An array of Facebook profile fields to grab on registration 
        # more info at http://wiki.developers.facebook.com/index.php/Users.getInfo
        profile_fields: [first_name, last_name]
        profile_class: SfGuardUserProfilePeer # The class to look for a create method in
        # The method to call in the profile_class.
        # Basically, customize this method to create users how you'd like
        create_method: createFbUser

So at this point, you've created a Facebook Application, installed the plugin, and have it configured.

The last step is to include the Facebook Connect dependencies.

A full guide is available here http://wiki.developers.facebook.com/index.php/Connect/Setting_Up_Your_Site


  1. Create a file called "xd_receiver.htm" in your web directory. Paste the following into the file:

  2. In your layout file, change the tag to look like

      <html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
  3. Inside the tag of your layout add


Now that everything is loaded, using the FB Connect stuff is pretty straightforward.

The easiest way is to use the default Facebook FBML.

Stick the following anywhere you want users to be able to sign up or login from.

      <fb:login-button onlogin="window.location='<?php url_for("fb_connect_auth");'"></fb:login-button>

You'll also need to stick the following JavaScript on the same page.

      var isAuthenticated = <?php echo ($sf_user->isAuthenticated() ? "true" : "false"); ?>;

      // we set a no_login attribute on logout 
      // so that fb connect users that logout aren't stuck constantly getting logged in
      var noAutologin = <?php echo ($sf_user->getAttribute("no_login", false) ? "true" : "false"); ?>;

           FB.Facebook.init("<?php echo sfConfig::get("app_facebook_api_key"); ?>",
           var api = FB.Facebook.apiClient;

           // if the user is connected do something
                 fbIsConnected = true;
                  if(!isAuthenticated && !noAutologin){
                      window.location = "<?php echo url_for("fb_connect_auth"); ?>";

You'll also probably want to customize the function that is specified by "create_method".

Ours looks like

        // $fbId will be the users Facebook ID
        // $profileFields is an array returned from Facebook's getUserInfo()
        public static function createFbUser( $fbId, $profileFields ){

          $sfUser = new sfGuardUser();
          $sfUser->setUserName( $email );

          $profile = $sfUser->getProfile();
          $profile->setFirstName( $profileFields["first_name"] );
          $profile->setLastName( $profileFields["last_name"] );

          return $sfUser->getId();

Basically, customize the function to do whatever application specific configuration you normally do.

You can also use it to pre-populate stuff from a user's Facebook account.

One last thing, if a user has all ready been authenticated with sfGuard

and then they FB Connect, the plugin will link their sfGuard user with their Facebook account.

Anyway, check out the Facebook Connect documentation for a better idea of how to do different things.