Skip to content

Instantly share code, notes, and snippets.

@Glitchii
Created May 1, 2023 16:02
Show Gist options
  • Save Glitchii/46dcc0bb0bc8afab2d9a8ca88b8ff6c7 to your computer and use it in GitHub Desktop.
Save Glitchii/46dcc0bb0bc8afab2d9a8ca88b8ff6c7 to your computer and use it in GitHub Desktop.
Login with Google - PHP
<?php
require __DIR__ . '/../vendor/autoload.php';
try {
$error = null;
$googleClientId = 'YOUR_GOOGLE_CLIENT_ID';
$userData = [];
if (isset($_POST['credential'], $_POST['g_csrf_token'])) {
//==========================
// Validate the CSRF token.
//==========================
if (!isset($_COOKIE['g_csrf_token']) || $_COOKIE['g_csrf_token'] != $_POST['g_csrf_token']) {
throw new \Exception('Google CSRF token validation failed.');
}
//===============
// Login process
//===============
$googleClient = new \Google_Client(['client_id' => $googleClientId]);
$payload = $googleClient->verifyIdToken($_POST['credential']);
// Validate the payload
if (!is_array($payload) ||
'https://accounts.google.com' != $payload['iss'] ||
time() < $payload['nbf'] ||
time() > $payload['exp'] ||
$googleClientId != $payload['aud']) {
throw new \Exception('Invalid Google ID token.');
}
// Get the user data
$userData = [
'UserID' => $payload['sub'],
'Email' => $payload['email'],
'EmailIsVerified' => $payload['email_verified'] ? 'Yes' : 'No',
'Name' => $payload['name'],
'Picture' => $payload['picture']
];
}
} catch (\Exception $e) {
$error = $e->getMessage();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="description" content="">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:,">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
<title>Login with Google</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h2>Login / Signin</h2>
<?php if ($error) { echo '<p style="color:red;">' . $error . '</p>'; } ?>
<div id="g_id_onload"
data-client_id="<?php echo $googleClientId; ?>"
data-context="signin"
data-ux_mode="popup"
data-login_uri="<?php echo 'https://YOUR_DOMAIN/login.php'; ?>"
data-auto_prompt="false">
</div>
<div class="g_id_signin"
data-type="standard"
data-shape="rectangular"
data-theme="filled_blue"
data-text="signin_with"
data-size="large"
data-width="250"
data-logo_alignment="left">
</div>
</div>
</div>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<?php if (count($userData)) {
echo '<h2>User Data</h2>';
echo '<ul>';
foreach ($userData as $key => $val) {
echo "<li>$key: $val</li>";
}
echo '</ul>';
} ?>
</div>
</div>
</div>
<script src="https://accounts.google.com/gsi/client" async defer></script>
<script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment