Google Ad

Gigya Social Network CakePHP Plugin

Date: Fri, May 28th 2010, 16:01 Author: nick Views: 40542 Comments share

CakePHP Gigya Plugin

Get it:
  • Download Now
  • git clone git:// gigya
Over the course of the last few weeks, I've been developing a complete social network plugin for CakePHP. The plugin takes advantage of Gigya, a free social network service that allows a developer a single API to interface with multiple social networks, including Facebook, Twitter, Linkedin, Google, OpenID, Yahoo, Myspace, and many more.
Gigya CakePHP Plugin is funded and co-developed by FlickEvents has been nice enough to allow me to open source the plugin for the CakePHP community.

The goal

The goal of this plugin is to integrate Gigya's social networking completely with CakePHP Auth component. This means, if everything is setup correctly (and the user allows it), you will be able to get detailed information about your users through all their social networks by using their Auth user_id.

  1. //show all connected social networks for the logged in user
  2. $details = GigyaUtil::api('getUserInfo', array(
  3.   'uid' => $this->Auth->user('id')
  4. ));

On top of that, the plugin also acts as a single click registration through a user's social network. Callbacks and overwrites are written within the framework of the plugin. This allows the developer to fine tune any part of the login/logout/connect/user_creation through the use of the built in callbacks.

At the end of the day, by implementing the Gigya Plugin, you are:
- Allowing your users to link their site account to their social networks.
- Allow simple one-click registration.
- Gain detailed information about your user's friends, photos, status
- Post status updates on behalf of your users
- and much more...

To review a full least of features, please review the Gigya site.

Enough talk, lets get it going!


Installation is straight forward, you'll need to:

- Copy the plugin into app/plugins/gigya

- Copy gigya/config/gigya.php.default to app/config/gigya.php and fill out the details.
  1. //app/config/gigya.php
  2. $config = array(
  3.   'Gigya' => array(
  4.     'apiKey' => 'GIGYA API KEY',
  5.     'secret' => 'GIGYA SECRET KEY'
  6.   )
  7. );

- Run the Schema into your database.
  1. cake schema create -plugin gigya


You'll need to signup for a free Gigya account and configure all your social networking apps to let gigya handle the connection/login/posting/etc.. callbacks. Gigya will set up all your apps for free, but it will take ~1 week to have them do it for you. I suggestion just doing it yourself. It's easy, and they've written full tutorials on how to do it, complete with screenshots.

Start Here:


The start to any actions the user partakes in starts with the included Gigya helper.
  1. //some controller
  2. var $helpers = array('Gigya.Gigya');

You will need to load the required scripts in the head of your layout and use any of the many built in Gigya login/connect/logout/etc.. widgets within your app with the helper.

Example Layout:
  1. //views/layouts/default.ctp
  2. <html>
  3. <head>
  4.   <?= $gigya->loader(); ?>
  5. </head>
  6. <body>
  7.   <?php
  8.   //Pseudo code, check if user is logged in or not, usually via Auth.
  9.   if(!$user_id_logged_in){
  10.     echo $gigya->login();
  11.   } else {
  12.     $html->link('Logout', array(
  13.       'plugin' => 'gigya',
  14.       'controller' => 'socialize',
  15.       'action' => 'logout'
  16.     ));
  17.   }
  18.   ?>
  19. </body>
  20. </html>

You can customize the look and feel of the login/connect widget with tons of options, including custom callbacks both inline (javascript) or through your CakePHP app (url redirects). For a full list of available options you can pass into $gigya->login() review:

Some useful examples:
  1. //Load the widget in a container (default is a popup)
  2. <div id="login-container"></div>
  3. <?= $gigya->login(array(
  4.          'containerID' => 'login-container'
  5.        )); ?>
  7. //Only allow facebook, twitter, or linkedin logins/connect
  8. <?= $gigya->login(array(
  9.          'enabledProviders' => 'facebook,twitter,linkedin'
  10.        )); ?>
  12. //Allow everything EXCEPT a certain provicer
  13. <?= $gigya->login(array(
  14.          'disabledProviders' => 'myspace'
  15.        )); ?>
  17. //Set height and width and add a style
  18. //styles: standard (default), blue, fullLogo
  19. <?= $gigya->login(array(
  20.          'height' => '300',
  21.          'width' => '500',
  22.          'buttonsStyle' => 'fullLogo'
  23.        )); ?>

By default, any login will redirect the user to the login action of the plugin. This action will decide how to handle the social network connection.

NOTE: You can change this behavior by passing Routes::url parseable redirectURL param.

  1. $gigya->login(array(
  2.   'redirectURL' => array(
  3.      'controller' => 'gigyas',
  4.      'action' => 'custom_login'
  5.   )
  6. ));

However, this is not recommened, it is recommened to let the plugin handle the logged in user as it does some advanced introspection to determine what to do with the login (ie, link the auth_user_id to the gigya_id, create a new user, or return a valid user_id because the link has already been made).

Login Flow

I've created a simple flowchart to show you how a gigya login click is handled internally by the plugin.

Gigya Flow Chart
You can alter the behavior of the flow by defining action callbacks.

Available Callbacks

It is important to note that throughout the flow you can bypass or alter the flow with specific callbacks you can define in app_controller.php

  1. /**
  2. * hands the authenticated user in, if the function returns a
  3. * valid $user_id the internal handle_user function will be
  4. * shortcutted proceeding straight to linking the user_id
  5. * to Gigya
  6. *
  7. * @param authenticated social network user
  8. * @return mixed user_id or boolean false to proceed
  9. */
  10. function beforeGigyaLogin($user){
  11.   //return valid user_id or false
  12. }
  14. /**
  15. * Preform some needed logic after a successful login
  16. *
  17. * @param authenticated social network user
  18. * @return void
  19. */
  20. function afterGigyaLogin($user){
  21.   //Do something with the user if need be.
  22. }
  24. /**
  25. * Preform some needed logic before a logout
  26. */
  27. function beforeGigyaLogout(){
  28.   //Do something...
  29. }
  31. /**
  32. * Allow the developer to decide how to create the user
  33. * instead of the Gigya plugin guessing what to do
  34. * by introspection on the Auth Component
  35. *
  36. * Defining this callback is preferable to the plugin
  37. * guessing how your users table is constructed.  
  38. * Although the plugin does a good job of creating a valid
  39. * user for you, its always nicer to do it yourself to be
  40. * sure there are no errors.
  41. *
  42. * @param authenticated gigya user
  43. * @return mixed user_id of created user, or false to let plugin decide.
  44. */
  45. function gigyaCreateUser($user){
  46.   //create the new user and return the created user_id;
  47. }

Linking Accounts to Gigya

Upon a successful login, the user_id along with the gigya_uuid will be saved to the gigyas table and linked to gigya. The benefit of linking the
user_id to the gigya_uuid is you can use the GigyaApi to make gigya calls based on your local user_id instead of gigya's uuid. This is a much nicer approach to retrieving details or preforming actions on behalf of your users.

Some examples using the built in GigyaUtil:

  1. //make sure to import the GigyaUtil somewhere before making calls
  2. App::import('Lib', 'Gigya.GigyaUtil');
  4. //get the details of all the social networks the user granted you access
  5. $details = GigyaUtil::api('getUserInfo', array(
  6.   'uid' => $user_id
  7. ));
  9. //Set the status for your user on ALL their social networks
  10. $result = GigyaUtil::api('setStatus', array(
  11.   'uid' => $user_id,
  12.   'status' => 'Posting from the Gigya Plugin!'
  13. ));

That's it!

There are tons more things you can do natively with the plugin, as its more of a framework to build your custom gigya integration. Anything you can do via the Gigya API you can do with this plugin.

I encourage you to look over the source, test cases, and of course the gigya developers area. is a good place to start to see what all you can do with this plugin.

As always, comments are much appreciated.