HybridAuth Integration
In this tutorial you will be able to set up social login to your web application .
You have to download hybridauth library.
The will be two files for hybridauth integration . The one is config file in that you have to enter the details of network such as key and secret . And the other one is callback , In that the system will validate the details and if the user is new the data can be stored to database , If existing user login then session can be allocated.
Config.php
$config =array(
//the location of hybridauth library
"base_url" => '/lib/hybridauth/',
"providers" => array (
//replace id and secret with the data received from the network
"Google" => array (
"enabled" => true,
"keys" => array ( "id" => "XXXXXXXXXX", "secret" => "XXXXXXXXXX" ),
),
"Facebook" => array (
"enabled" => true,
"keys" => array ( "id" => "XXXXXXXXX", "secret" => "XXXXXXXX" ),
),
"Twitter" => array (
"enabled" => true,
"keys" => array ( "key" => "XXXXXXXX", "secret" => "XXXXXXX" )
),
),
// if you want to enable logging, set 'debug_mode' to true then provide a writable file by the web server on "debug_file"
"debug_mode" => false,
"debug_file" => "",
);
include('lib/hybridauth/Hybrid/Auth.php');
Auth.php
session_start();
include 'config.php';
if(isset($_GET['provider']))
{
$provider = $_GET['provider'];
try{
$hybridauth = new Hybrid_Auth( $config );
$authProvider = $hybridauth->authenticate($provider);
$user_profile = $authProvider->getUserProfile();
if($user_profile && isset($user_profile->identifier))
{
/*
Checking for the user at database
*/
$sql = "SELECT * FROM users WHERE email = :email";
$params = array(':email' => $user_profile->email );
$data = query($sql,$params);
$info = $data->fetch();
if($data->rowCount() == 0)
{
//New User then insert data and place the user id to session then redirect to dashboard
$_SESSION["user_id"] = $info['id'];
redirect('dashboard.php');
}
else
{
//existing user allocate session and redirect to'dashboard
$_SESSION["dz_user_id"] = $info['id'];
redirect('index.php?a=dashboard');
}
}
}
catch( Exception $e )
{
switch( $e->getCode() )
{
case 0 : echo "Unspecified error."; break;
case 1 : echo "Hybridauth configuration error."; break;
case 2 : echo "Provider not properly configured."; break;
case 3 : echo "Unknown or disabled provider."; break;
case 4 : echo "Missing provider application credentials."; break;
case 5 : echo "Authentication failed. "
. "The user has canceled the authentication or the provider refused the connection.";
break;
case 6 : echo "User profile request failed. Most likely the user is not connected "
. "to the provider and he should to authenticate again.";
$twitter->logout();
break;
case 7 : echo "User not connected to the provider.";
$twitter->logout();
break;
case 8 : echo "Provider does not support this feature."; break;
}
// well, basically your should not display this to the end user, just give him a hint and move on..
echo "Original error message: " . $e->getMessage();
echo "
<hr />
Trace
<pre>" . $e->getTraceAsString() . "</pre>
";
}
}
Login.php
Sign in with Google
Sign in with Facebook
Sign in with Twitter
In this tutorial two functions is used .
redirect() – redirect to another page
query() – sql queries run
Recent Comments