Last active
February 27, 2021 22:37
-
-
Save Jerry0022/27248ce51369e261829a to your computer and use it in GitHub Desktop.
Google oAuth2, sign up, sign in, logout and show user data. Need to set REDIRECT_URL from google developer console and the https://github.com/google/google-api-php-client cloned in the web directory.
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 | |
// Enable error reporting | |
error_reporting(E_ALL); | |
ini_set('display_errors', 1); | |
$google_redirect_url = 'REDIRECT_URL'; | |
//start session | |
session_start(); | |
//include google api files | |
include_once 'google-api-php-client/src/Google/autoload.php'; | |
// New Google client | |
$gClient = new Google_Client(); | |
$gClient->setApplicationName('ApplicationName'); | |
$gClient->setAuthConfigFile('client_secret.json'); | |
$gClient->addScope(Google_Service_Oauth2::USERINFO_PROFILE); | |
$gClient->addScope(Google_Service_Oauth2::USERINFO_EMAIL); | |
// New Google Service | |
$google_oauthV2 = new Google_Service_Oauth2($gClient); | |
// LOGOUT? | |
if (isset($_REQUEST['logout'])) | |
{ | |
unset($_SESSION["auto"]); | |
unset($_SESSION['token']); | |
$gClient->revokeToken(); | |
header('Location: ' . filter_var($google_redirect_url, FILTER_SANITIZE_URL)); //redirect user back to page | |
} | |
// GOOGLE CALLBACK? | |
if (isset($_GET['code'])) | |
{ | |
$gClient->authenticate($_GET['code']); | |
$_SESSION['token'] = $gClient->getAccessToken(); | |
header('Location: ' . filter_var($google_redirect_url, FILTER_SANITIZE_URL)); | |
return; | |
} | |
// PAGE RELOAD? | |
if (isset($_SESSION['token'])) | |
{ | |
$gClient->setAccessToken($_SESSION['token']); | |
} | |
// Autologin? | |
if(isset($_GET["auto"])) | |
{ | |
$_SESSION['auto'] = $_GET["auto"]; | |
} | |
// LOGGED IN? | |
if ($gClient->getAccessToken()) // Sign in | |
{ | |
//For logged in user, get details from google using access token | |
try { | |
$user = $google_oauthV2->userinfo->get(); | |
$user_id = $user['id']; | |
$user_name = filter_var($user['givenName'], FILTER_SANITIZE_SPECIAL_CHARS); | |
$email = filter_var($user['email'], FILTER_SANITIZE_EMAIL); | |
$gender = filter_var($user['gender'], FILTER_SANITIZE_SPECIAL_CHARS); | |
$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>"; | |
$_SESSION['token'] = $gClient->getAccessToken(); | |
// Show user | |
echo '<br /><a href="'.$profile_url.'" target="_blank"><img src="'.$profile_image_url.'?sz=100" /></a>'; | |
echo '<br /><a class="logout" href="?logout=1">Logout</a>'; | |
$boolarray = Array(false => 'false', true => 'true'); | |
echo '<p>Was automatical login? '.$boolarray[isset($_SESSION["auto"])].'</p>'; | |
//list all user details | |
echo '<pre>'; | |
print_r($user); | |
echo '</pre>'; | |
} catch (Exception $e) { | |
// The user revoke the permission for this App! Therefore reset session token | |
unset($_SESSION["auto"]); | |
unset($_SESSION['token']); | |
header('Location: ' . filter_var($google_redirect_url, FILTER_SANITIZE_URL)); | |
} | |
} | |
else // Sign up | |
{ | |
//For Guest user, get google login url | |
$authUrl = $gClient->createAuthUrl(); | |
// Fast access or manual login button? | |
if(isset($_GET["auto"])) | |
{ | |
header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL)); | |
} | |
else | |
{ | |
echo '<p>Login?</p>'; | |
echo '<a class="login" href="'.$authUrl.'"><img src="images/google-login-button.png" /></a>'; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$gClient->revokeToken();
It does't work without argument now. At least for me.
I had to change it to:
$gClient->revokeToken($_SESSION['token']);
And of coz unset $_SESSION['token'] after revokeToken. Not before.