Last active
December 19, 2015 19:38
-
-
Save jainsiddharth21/6007293 to your computer and use it in GitHub Desktop.
Login with Google in CakePHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//HTML page start | |
echo '<h1>Login with Google</h1>'; | |
if(isset($authUrl)) //user is not logged in, show login button | |
{ | |
echo '<a class="login" href="'.$authUrl.'"><img src="'.WEB_ROOT.'google-login-button.png" /></a>'; | |
} | |
else | |
{ | |
echo $msg; | |
echo '<p><a class="logout" href="?reset=1">Logout</a></p>'; | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (!defined('WEB_ROOT')) | |
{ | |
define('WEB_ROOT', "http://".$_SERVER['HTTP_HOST']."/creativepeople/images/"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class User extends AppModel | |
{ | |
var $name = 'User'; | |
/* The $useTable keyword is reserved in cakePHP | |
This is used to define the custom new table name */ | |
var $useTable = 'google_users'; | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class UsersController extends AppController | |
{ | |
function login() | |
{ | |
########## Google Settings.. Client ID, Client Secret ############# | |
$google_client_id = 'xxxxxxxxxxxx.apps.googleusercontent.com'; | |
$google_client_secret = 'XXXXXXXXXXXXXXXXXXXXXXXX'; | |
$google_redirect_url = 'http://localhost/google-login/'; | |
$google_developer_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; | |
//include google api files | |
require_once 'src/Google_Client.php'; | |
require_once 'src/contrib/Google_Oauth2Service.php'; | |
$gClient = new Google_Client(); | |
$gClient->setApplicationName('Login to localhost'); | |
$gClient->setClientId($google_client_id); | |
$gClient->setClientSecret($google_client_secret); | |
$gClient->setRedirectUri($google_redirect_url); | |
$gClient->setDeveloperKey($google_developer_key); | |
$google_oauthV2 = new Google_Oauth2Service($gClient); | |
//If user wish to log out, we just unset Session variable | |
if (isset($_REQUEST['reset'])) | |
{ | |
$this->set('msg', 'Logout'); | |
//unset($_SESSION['token']); | |
$this->Session->delete('token'); | |
$gClient->revokeToken(); | |
header('Location: ' . filter_var($google_redirect_url, FILTER_SANITIZE_URL)); | |
} | |
//Redirect user to google authentication page for code, if code is empty. | |
//Code is required to aquire Access Token from google | |
//Once we have access token, assign token to session variable | |
//and we can redirect user back to page and login. | |
if (isset($_REQUEST['code'])) | |
{ | |
$gClient->authenticate($_REQUEST['code']); | |
$this->Session->write('token', $gClient->getAccessToken()); | |
$this->redirect(filter_var($google_redirect_url, FILTER_SANITIZE_URL), null, false); | |
//header('Location: ' . filter_var($google_redirect_url, FILTER_SANITIZE_URL)); | |
return; | |
} | |
if ($this->Session->read('token')) | |
{ | |
$gClient->setAccessToken($this->Session->read('token')); | |
} | |
if ($gClient->getAccessToken()) | |
{ | |
//Get user details if user is logged in | |
$user = $google_oauthV2->userinfo->get(); | |
$user_id = $user['id']; | |
$user_name = filter_var($user['name'], FILTER_SANITIZE_SPECIAL_CHARS); | |
$email = filter_var($user['email'], FILTER_SANITIZE_EMAIL); | |
$profile_url = filter_var($user['link'], FILTER_VALIDATE_URL); | |
$profile_image_url = filter_var($user['picture'], FILTER_VALIDATE_URL); | |
$personMarkup = "$email<div><img src='$profile_image_url?sz=50'></div>"; | |
$this->Session->write('token', $gClient->getAccessToken()); | |
} | |
else | |
{ | |
//get google login url | |
$authUrl = $gClient->createAuthUrl(); | |
} | |
if(isset($authUrl)) //user is not logged in, show login button | |
{ | |
$this->set('authUrl', $authUrl); | |
} | |
else // user logged in | |
{ | |
/* Save data in db */ | |
/* And set variables for to display or redirect some where else */ | |
$result = $this->User->find('count', array('conditions' => array('google_id' => $user_id))); | |
if($result > 0) | |
{ | |
$msg = 'Welcome back '.$user_name.'!<br />'; | |
$msg .= '<br />'; | |
$msg .= '<img src="'.$profile_image_url.'" width="100" align="left" hspace="10" vspace="10" />'; | |
$msg .= '<br />'; | |
$msg .= ' Name: '.$user_name.'<br />'; | |
$msg .= ' Email: '.$email.'<br />'; | |
$msg .= '<br />'; | |
$this->set('msg', $msg); | |
} | |
else | |
{ | |
$msg1 = 'Hi '.$user_name.', Thanks for Registering!'; | |
$msg1 .= '<br />'; | |
$msg1 .= '<img src="'.$profile_image_url.'" width="100" align="left" hspace="10" vspace="10" />'; | |
$msg1 .= '<br />'; | |
$msg1 .= ' Name: '.$user_name.'<br />'; | |
$msg1 .= ' Email: '.$email.'<br />'; | |
$msg1 .= '<br />'; | |
$this->set('msg', $msg1); | |
$this->User->query("INSERT INTO google_users (google_id, google_name, google_email, google_link, google_picture_link) VALUES ($user_id, '$user_name', '$email', '$profile_url', '$profile_image_url')"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment