social_provider && $provider_user_id != $row->social_provider_id) { $q1 = "UPDATE ind_users SET social_provider = '".$provider_name."' , social_provider_id = '".$provider_user_id."' WHERE id = '".$row->id."'"; $result1=mysql_query($q1) or die(mysql_error()); } return $row; } /* * get the user data from database by provider name and provider user id **/ function create_new_hybridauth_user($user_profile, $provider_name) { $social_provider = $provider_name; $social_provider_id = $user_profile->identifier; $email = $user_profile->email; // let generate a random password for the user $password = md5(md5(str_shuffle( "0123456789abcdefghijklmnoABCDEFGHIJ") )); $first_name = $user_profile->firstName; $last_name = $user_profile->lastName; $phone = $user_profile->phone; $profile_image = $user_profile->photoURL; $gender = $user_profile->gender; $created = date('Y-m-d'); $data="INSERT INTO ind_users ( social_provider, social_provider_id, email, password, first_name, last_name, phone, profile_image, gender, created ) VALUES( '$social_provider', '$social_provider_id', '$email', '$password', '$first_name', '$last_name', '$phone', '$profile_image', '$gender', '$created')"; $insert=mysql_query($data) or die(mysql_error()); } // if page requested by submitting login form if( isset($_REQUEST["user_email"] ) && isset( $_REQUEST["user_password"] ) ) { $user_exist = get_user_by_email_and_password( $_REQUEST["user_email"], $_REQUEST["user_password"] ); // user exist? if( $user_exist ) { // set the user as connected and redirect him to a home page or something $_SESSION["user_connected"] = true; header("Location: index.php"); exit; } // wrong email or password? else { // redirect him to an error page header("Location: login.php"); exit; } } // else, if login page request by clicking a social provider button elseif(isset( $_REQUEST["provider"] ) && ($_REQUEST["provider"] == "Facebook" || $_REQUEST["provider"] == "Google")) { // Remove AND condition if use multiple provider // the selected provider $provider_name = $_REQUEST["provider"]; try { // change the following paths if necessary $config = dirname(__FILE__) . '/vendor/hybridauth/config.php'; require_once( "vendor/hybridauth/Hybrid/Auth.php" ); // 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(); } // something went wrong? catch( Exception $e ){ header("Location: login.php"); exit; } /*if($_REQUEST["provider"] == "Google") { pr($user_profile); exit; }*/ // check if the current user already have authenticated using this provider before $user_exist = get_user_by_provider_and_id( $provider_name, $user_profile->identifier, $user_profile->email ); // if the used didn't authenticate using the selected provider before // we create a new entry on database.users for him if(empty($user_exist)) { create_new_hybridauth_user($user_profile, $provider_name); // set the user as connected and redirect him $_SESSION["user_email"] = $user_profile->email; $_SESSION["first_name"] = $user_profile->firstName; $_SESSION["phone"] = $user_profile->phone; $_SESSION["profile_image"] = $user_profile->photoURL; } else { // set the user as connected and redirect him $_SESSION["user_email"] = $user_exist->email; $_SESSION["phone"] = $user_exist->phone; $_SESSION["first_name"] = $user_exist->first_name; $_SESSION["profile_image"] = $user_exist->photoURL; } // set the user as connected and redirect him $_SESSION["user_connected"] = true; header("Location: index.php"); exit; }?>