SSO Login with Gmail
SSO (Single sign on) login.
Question : How to login with Gmail?
Answer : There are few steps for solution of this.
Step1 : open this link: https://console.developers.google.com/
Step2 : OAuth consent screen (From the left pannel)
Step3: Credential
Step4:
composer require google/apiclient:"^2.0"
Step5: Save this file as: google-sso-config.php
and paste this code
<?php
//start session on web page
session_start ();
//config.php
//Include Google Client Library for PHP autoload file
require_once 'vendor/autoload.php' ;
//Make object of Google API Client for call Google API
$google_client = new Google_Client ();
//Set the OAuth 2.0 Client ID
$google_client ->setClientId ('YOUR-CLIENT-ID.apps.googleusercontent.com' );
//Set the OAuth 2.0 Client Secret key
$google_client ->setClientSecret (' Client secret ' );
//Set the OAuth 2.0 Redirect URI
$google_client ->setRedirectUri ('http://localhost/bharatbaba.com' );
// to get the email and profile
$google_client ->addScope ('email' );
$google_client ->addScope ('profile' );
? >
Step6: Save this file as: google-sso-button.php
and paste this code
<?php
include ('google-sso-config.php' );
$login_button = '' ;
if (isset ($_GET ["code" ]))
{
$token = $google_client ->fetchAccessTokenWithAuthCode ($_GET ["code" ]);
if (!isset ($token ['error' ]))
{
$google_client ->setAccessToken ($token ['access_token' ]);
$_SESSION ['access_token' ] = $token ['access_token' ];
$google_service = new Google_Service_Oauth2 ($google_client );
$data = $google_service ->userinfo ->get ();
if (!empty ($data ['given_name' ]))
{
$_SESSION ['user_first_name' ] = $data ['given_name' ];
}
if (!empty ($data ['family_name' ]))
{
$_SESSION ['user_last_name' ] = $data ['family_name' ];
}
if (!empty ($data ['email' ]))
{
$_SESSION ['user_email_address' ] = $data ['email' ];
}
if (!empty ($data ['gender' ]))
{
$_SESSION ['user_gender' ] = $data ['gender' ];
}
if (!empty ($data ['picture' ]))
{
$_SESSION ['user_image' ] = $data ['picture' ];
}
echo "Jay google data=<pre>" ; print_r ($data ); echo "</pre>" ;
}
}
if (!isset ($_SESSION ['access_token' ]))
{
$login_button = '<a href="' .$google_client ->createAuthUrl ().'">Login With Google</a>' ;
}
? >
<div class ="w3-container w3-content w3-center w3-padding-64" style =" max-width:800px" >
<h2 align ="center" > GOOGLE SSO Login PHP</h2>
<div >
<?php
if ($login_button == '' )
{
echo '<div class="center">Welcome User</div><div class="center">' ;
echo '<img src="' .$_SESSION ["user_image" ].'" class="center" />' ;
echo '<h2><b>Name :</b> ' .$_SESSION ['user_first_name' ].' ' .$_SESSION ['user_last_name' ].'</h3>' ;
echo '<h2><b>Email :</b> ' .$_SESSION ['user_email_address' ].'</h3>' ;
echo '<h2><a href=" google-sso-logout.php ">Logout</h3></div>' ;
}
else
{
echo '<div align="center">' .$login_button . '</div>' ;
}
? >
</div>
</div>
Step7: Save this file as: google-sso-logout .php
and paste this code
<?php
include ('google-sso-config.php' );
//Reset token
$google_client ->revokeToken ();
session_destroy ();
header ('location:index.php' );
? >
https://www.youtube.com/watch?v=PUnldg53TnE
VIDEO
Note : This code is required PHP version 8.1.0. I have upgraded my PHP once I received below error.
Fatal error: Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.1.0". You are running 8.0.28. in /Applications/XAMPP/xamppfiles/htdocs/bharatbaba.com/vendor/composer/platform_check.php on line 24
Share on Facebook