setClientId(GOOGLE_CLIENT_ID);
$client->setClientSecret(GOOGLE_CLIENT_SECRET);
$client->setRedirectUri(GOOGLE_REDIRECT_URL);
$client->addScope("email");
$client->addScope("profile");
// Handle the OAuth 2.0 server response
if (isset($_GET['code'])) {
try {
error_log('Google callback received: ' . print_r($_GET, true));
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
error_log('Google token response: ' . print_r($token, true));
if (isset($token['error'])) {
throw new Exception('Token error: ' . ($token['error_description'] ?? 'Unknown error'));
}
$client->setAccessToken($token['access_token']);
$google_oauth = new Google_Service_Oauth2($client);
$google_account_info = $google_oauth->userinfo->get();
$userInfo = [
'email' => $google_account_info->email,
'name' => $google_account_info->name,
];
$_SESSION['google_user_info'] = $userInfo;
// Explicitly save the session data before redirecting.
session_write_close();
header('Location: auth_handler.php');
exit();
} catch (Throwable $t) {
// Log the actual error to the server's error log for inspection.
error_log('Google Auth Exception: ' . $t->getMessage());
header('Location: login.php?error=google_auth_failed_exception');
exit();
}
} else {
$authUrl = $client->createAuthUrl();
// Instead of redirecting, print the URL for debugging
echo "Please copy this URL and send it back to me:
";
echo $authUrl;
exit();
}