Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active August 6, 2023 06:06
Show Gist options
  • Save code-boxx/000837d80b10df6b289764e829beb74e to your computer and use it in GitHub Desktop.
Save code-boxx/000837d80b10df6b289764e829beb74e to your computer and use it in GitHub Desktop.
PHP Login With Google

PHP LOGIN WITH GOOGLE

https://code-boxx.com/php-login-with-google/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

GOOGLE CLOUD CONSOLE SETUP

https://console.cloud.google.com/apis/dashboard

  1. Create a new project.
  2. Create project (or select existing one).
  3. OAuth consent screen.
  • Provide all the details of your app/website.
  • Scope can be left empty.
  • Add your own gmail account as test users.
  • DO NOT PUBLISH APP UNTIL YOU ARE READY.
  1. Credentials.

DOWNLOAD API LIBRARY

  1. Install Composer https://getcomposer.org/
  2. Open command prompt.
  3. Navigate to project folder - cd PATH/MY/PROJECT/
  4. Run composer require google/apiclient
<?php
// (A) LOAD GOOGLE CLIENT LIBRARY
require("vendor/autoload.php");
// (B) NEW GOOGLE CLIENT
$goo = new Google\Client();
$goo->setClientId("YOUR-ID");
$goo->setClientSecret("YOUR-SECRET");
$goo->addScope("email");
$goo->addScope("profile");
$goo->setRedirectUri("http://localhost/3-login.php");
<?php
// (A) ALREADY SIGNED IN
session_start();
if (isset($_SESSION["token"])) {
header("Location: 4-home.php"); exit;
}
// (B) ON LOGIN - PUT TOKEN INTO SESSION
require "2-google.php";
if (isset($_GET["code"])) {
$token = $goo->fetchAccessTokenWithAuthCode($_GET["code"]);
if (!isset($token["error"])) {
$_SESSION["token"] = $token;
header("Location: 4-home.php"); exit;
}
}
// (C) SHOW LOGIN PAGE ?>
<!DOCTYPE html>
<html>
<head>
<title>Login With Google</title>
</head>
<body>
<?php if (isset($token["error"])) { ?>
<div><?= print_r($token); ?></div>
<?php } ?>
<a href="<?= $goo->createAuthUrl() ?>">Login with Google</a>
</body>
</html>
<?php
// (A) NOT LOGGED IN
session_start();
if (!isset($_SESSION["token"])) {
header("Location: 3-login.php"); exit;
}
// (B) TOKEN EXPIRED - TO LOGIN PAGE
require "2-google.php";
$goo->setAccessToken($_SESSION["token"]);
if ($goo->isAccessTokenExpired()) {
unset($_SESSION["token"]);
header("Location: 3-login.php"); exit;
}
// (C) GET USER PROFILE
$user = (new Google_Service_Oauth2($goo))->userinfo->get();
print_r($user);
<?php
// (A) NOT LOGGED IN
session_start();
if (!isset($_SESSION["token"])) {
header("Location: 3-login.php"); exit;
}
// (B) REMOVE & REVOKE TOKEN
require "2-google.php";
$goo->setAccessToken($_SESSION["token"]);
$goo->revokeToken();
unset($_SESSION["token"]);
// REMOVE YOUR OWN USER SESSION VARIABLES AS WELL
header("Location: 3-login.php"); exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment