Cakephp

Tutorials




PHP Developer

Houston Texas

PHP Developer

Atlanta Georgia

Senior PHP Software Engineer

Midtown Atlanta Georgia


Single click login into Google use api with in PHP / CakePHP

Google Login API

We are going to login to our Cakephp 2 website (which does not use composer) with a Google api. We are going to update our Social user's table and our normal users table. Basically if a user exist we are going to update other wise add the user.

Our first table is:

CREATE TABLE IF NOT EXISTS `user_socials` (
  `id` int(11) NOT NULL,
  `oauth_provider` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `oauth_uid` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `link` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `picture` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Our users table is:

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL,
  `user_role_id` int(1) NOT NULL,
  `firstname` varchar(100) NOT NULL,
  `lastname` varchar(100) NOT NULL,
  `google_id` varchar(255) DEFAULT NULL,
  `facebook_id` varchar(255) DEFAULT NULL,
  `linkedin_id` varchar(255) DEFAULT NULL,
  `email` varchar(100) NOT NULL,
  `password` varchar(100) NOT NULL,
  `created` date NOT NULL,
  `modified` date NOT NULL
) ENGINE=MyISAM AUTO_INCREMENT=1334 DEFAULT CHARSET=latin1;

we have optained the google api files at https://github.com/Skillbooker/google_oauth

The Google function

This google function has to go in your controller.

public function google() {
	
if(!session_id()) {
	session_start();
}


define("SITEURL", "http://www.skillbooker.com/");    
define("GOOGLE_KEY", "0000000000000000");
//your google key
define("GOOGLE_SECRET", "000000000000000");
//your google secret key
define("GOOGLE_REROUTE", "social/google");
	
require_once(APP . 'Vendor' . DS . 'googlelogin' . DS . 'Google_Client.php');
require_once(APP . 'Vendor' . DS . 'googlelogin' . DS . 'contrib' . DS . 'Google_Oauth2Service.php');    

######### edit details ##########
$clientId = GOOGLE_KEY;
$clientSecret = GOOGLE_SECRET;
$redirectUrl = SITEURL.GOOGLE_REROUTE;


$gClient = new Google_Client();
$gClient->setApplicationName('Login to Skillbooker.com');
$gClient->setClientId($clientId);
$gClient->setClientSecret($clientSecret);
$gClient->setRedirectUri($redirectUrl);
 
$google_oauthV2 = new Google_Oauth2Service($gClient);

if(isset($_GET['code'])){
    $gClient->authenticate($_GET['code']);
    $_SESSION['token'] = $gClient->getAccessToken();
}

if (isset($_SESSION['token'])) {
    $gClient->setAccessToken($_SESSION['token']);
}

if ($gClient->getAccessToken()) {
    //Get user profile data from google
    $userProfile = $google_oauthV2->userinfo->get();
	         
			//$_SESSION['google_profile'] = $userProfile;
            
			$social['oauth_provider'] = 'google'; 
			$social['oauth_uid'] = $userProfile['id'];        
            $social['picture']  = $userProfile['picture'];
            $social['link']  = $userProfile['link'];
            
            $user['lastname'] = $userProfile['family_name'];
            $user['firstname'] = $userProfile['given_name'];
            $user['username']  = $userProfile['name'];
			$user['email']  = $userProfile['email'];
			$user['user_role_id']  = $role;

            $email  = $userProfile['email'];
            
            $this->socialcheck($social, $user, $email);
	
	
	} else {
	
	$authUrl = $gClient->createAuthUrl();
	header('Location: ' . $authUrl);
	
	}

}

now the function that updates our social users tables and users tables

this function will be used for our facebook, google, linkedin, twitter and all other one click social login systems

function socialcheck($social, $user, $email) {
	
	$this->loadModel('UserSocial');
	$options = array('conditions' => array('UserSocial.oauth_uid' => $social['oauth_uid']));
	$findsocialuser = $this->UserSocial->find('first',$options);
	
	if(!empty($findsocialuser)){
	
		$userdata['id']	=	$findsocialuser['UserSocial']['id'];
		$userdata['modified']	=	date("Y-m-d H:i:s");
		
		$this->UserSocial->save($userdata,false);
		$social_id = $findsocialuser['UserSocial']['id'];
	
	} else {
		
		$userdata = $social;	
		$userdata['created']	=	date("Y-m-d H:i:s");
		$userdata['modified']	=	date("Y-m-d H:i:s");
		
		$this->UserSocial->save($userdata,false);      

	}
    
		$this->loadModel('User');
		
		$options = array('conditions' => array('User.email' => $email));
		$finduser = $this->User->find('first',$options);
		
		if($social['oauth_provider'] == 'google') { $data['google_id'] = $social['oauth_uid']; }
		if($social['oauth_provider'] == 'facebook') { $data['facebook_id'] = $social['oauth_uid']; }
		if($social['oauth_provider'] == 'linkedin') { $data['linkedin_id'] = $social['oauth_uid']; }
    
	if(!empty($finduser)){
		
		$data['id']	=	$finduser['User']['id'];
		$data['modified']	=	date("Y-m-d H:i:s");
	
		$this->User->save($data,false);
		
	} else {
			
		$data  = $user;
		// setting data to the user data that will contain the first names email addresses ect
		
		$data['password']	=	AuthComponent::password($user['firstname']);
		// creating a password for the user in our database -->this can be emailed to the user
	
		$this->User->save($data, false);
	
	}
}

 


Login/Register to view full article and leave comments and questions