Implementing Google account authentication is very popular these days. It is easy and powerful method for user authentication, Google account authentication takes less effort from user for registration or login on your system. there is no need for the user to remember the username and password.
Table Of Content
1 Prerequisites
1.) PHP version of >7.4
2.) Composer
3.) Google Account
2 Introduction
In this article, We are going to see how to implementation Google account authentication to your PHP application. We’ll be using Google Client Library.
3 Create Project Folder "google-signin-app"
Create Project Folder "google-signin-app" in root directory store all project files
4 Install Google Client Library
First, make sure your computer has a composer.
Use the following command to install Google Client Library via Composer.
composer require google/apiclient:^2.0
This command will download the google api client Library and add it to your project.
5 Create Google API Console Project
5.1 Login into Google Developer Account
If you have Google Developer Account Go to https://console.cloud.google.com/, else you can easily create one directly from the Google Developers Console.
5.2 Create New Project
Create a Project in the Google Developer Console
Entering Project information in the following Screen
Project successfully created with given information's
5.3 Create Credentials
Now Create the credential by choosing "Credentials" in the side bar, click "Create Credentials" button and choose "OAuth Client ID".
5.4 Configure Consent Screen
Now Click "Configure Consent Screen" Button.
It will redirected to the following screen and choose "External" option then create.
5.5 Update App Information
By providing App and Developer Information Complete the form then click "Save and Continue" Button.
5.6 OAuth Client Created
Now Create the OAuth Client by choosing "Credentials" in the side bar, click "Create Credentials" button and choose "OAuth Client ID".
It will redirected to the following screen and fill the detail about our app and fill the authorized redirect URIs. This is the URI that we will use to redirect user after they choose their Google account to login to our web. For example here I use http://localhost/google-signin-app/google-callback.php for the callback URI..
Now we get the Client ID and the Client Secret.
6 Configure Google App Credentials(config.php)
Insert the Client ID and Client Secret key and redirect URI into the config.php file, Which we obtained from previous step GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET. Additionally, include a new key in the config.php file called GOOGLE_REDIRECT_URI and populate it with the callback URI used in the Google API Console.
Create a simple login page (login.php) that includes a "Login with Google" button:
google-signin-app/login.php
<?php
include('config.php');
require_once 'vendor/autoload.php';
$client = new Google_Client();
$client->setClientId(GOOGLE_CLIENT_ID);
$client->setClientSecret(GOOGLE_CLIENT_SECRET);
$client->setRedirectUri(GOOGLE_REDIRECT_URI);
$client->addScope('email');
$client->addScope('profile');
$login_url = $client->createAuthUrl();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login with Google</title>
</head>
<body>
<h1>Login with Google</h1>
<a href="<?= htmlspecialchars($login_url) ?>">Login with Google</a>
</body>
</html>
8 Create Google Callback Page - google-callback.php
Create a file (google-callback.php) to handle the callback from Google after authentication
google-signin-app/google-callback.php
<?php
require_once 'vendor/autoload.php';
session_start();
include('config.php');
$client = new Google_Client();
$client->setClientId(GOOGLE_CLIENT_ID);
$client->setClientSecret(GOOGLE_CLIENT_SECRET);
$client->setRedirectUri(GOOGLE_REDIRECT_URI);
$client->addScope('email');
$client->addScope('profile');
if (isset($_GET['code'])) {
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
$client->setAccessToken($token['access_token']);
// Get user info
$oauth2 = new Google_Service_Oauth2($client);
$userInfo = $oauth2->userinfo->get();
// Store user info in session or database
$_SESSION['id'] = $userInfo->id;
$_SESSION['email'] = $userInfo->email;
$_SESSION['name'] = $userInfo->name;
// Redirect to a secured page or dashboard
header('Location: dashboard.php');
exit();
}
9 Create Dashboard - dashboard.php
Create a simple secured page (dashboard.php) that only logged-in users can access:
google-signin-app/dashboard.php