Let say we have an existen login system on a website on which we want to let users to authenticate using Facebook, Twitter and LinkedIn.

Lets also assume our databse.users table look like this:

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(200) NOT NULL,
  `password` varchar(200) NOT NULL,
  `first_name` varchar(200) NOT NULL,
  `last_name` varchar(200) NOT NULL,
  `created_at` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 ; 

and login.php form is something like this:

<form> 
	<fieldset>
		<legend>Sign-in form</legend>
		login   : <input type="text" name="login" /><br /> 
		password: <input type="password" name="password" /><br /> 

		<input type="submit" value="Sign-in" />
	<fieldset>
</form>

1. First we need some awesome Social sign in buttons to add to our forms, and we are going to use those created by komodomedia.com (you may also like these CSS social sign-in buttons made by samcollins).


2. Then we add three buttons for these social networks to our login form, and each one of them will link to login.php providing the selected provider name as parameter: login.php?provider={provider_name}

<fieldset>
    <legend>Or use anohter service</legend>

    <a href="login.php?provider=facebook"><img src="images/buttons/facebook.gif" /></a><br />
    <a href="login.php?provider=twitter" ><img src="images/buttons/twitter.gif"  /></a><br />
    <a href="login.php?provider=linkedin"><img src="images/buttons/linkedin.gif" /></a>
</fieldset>

3.. Add the couple (provider_name, provider_uid) to your databse.users to be able to identify them:

create unique index hybridauth_idx on databse.users (hybridauth_provider_name, hybridauth_provider_uid);

-- `hybridauth_provider_name` is the provider name if the user is signed up with hybridauth 
-- `hybridauth_provider_uid`  is the Unique user ID on the used provider. 

4. Next, we need to alter login.php flow by handling requested provider parameter:

<?php
	// if page requested by submitting login form
	// then we keep the same login flow 
	if( isset($_REQUEST["login"]) && isset($_REQUEST["password"]) ){
		$user_exist = get_user_by_login_and_password($_REQUEST["login"], $_REQUEST["password"]);

		// if user exist on database
		if( $user_exist ){ 
			// then create a session for the user whithin your application 
			// and redirect him back to the profile or dashboard page or something :)
			$_SESSION["user_connected"] = true;
			redirect_to("http://www.example.com/user/home");
		} 		
	}

	// else, if login page request by clicking a provider button
	elseif( isset($_REQUEST["provider"]) ){ 
		// the selected provider
		$provider_name = $_REQUEST["provider"];

		try{
			// initialize Hybrid_Auth with a given file
			$hybridauth = new Hybrid_Auth( $config );

			// try to authenticate with the selected provider
			$adapter = $hybridauth->authenticate( $provider_name );

			// then grab the user profile 
			$user_profile = $adapter->getUserProfile();
		}
		catch( Exception $e ){
			echo "Error: please try again!";
			echo "Original error message: " . $e->getMessage();
		}

		# and that's it!
		# beyond that, its up to you sign-in the user if he already exist on your database
		# or sign-up the user if not.
		# the following pseudocode is provided only as an example:

		$user_exist = get_user_by_provider_and_id($provider_name, $user_profile->identifier);

		// if user exist on database, then same as before
		if( $user_exist ){ 
			$_SESSION["user_connected"] = true;
			redirect_to("http://www.example.com/user/home");
		} 

		// if not, create a new one on database
		else{ 
			create_new_hybridauth_user(
				$provider_name,
				$user_profile->identifier, 
				$user_profile->email, 
				$user_profile->firstName, 
				$user_profile->lastName, 
				generate_password() 
			); 

			// flag user as connected whithin your application and redirect him to home page
			$_SESSION["user_connected"] = true;
			redirect_to("http://www.example.com/user/home");
		}
	}    

5. Finally we need define and implement get_user_by_provider_and_id() and create_new_hybridauth_user() to respectively find a user by a given unique index (provider_name, provider_uid) on databse.users, and to register new users connected via Hybridauth library.