PHP provide SDK that helps developers to add google login feature in application.
Let’s start to integrate google login using php sdk library in codeIgniter 4 applications:
Step 1: Set Up Google API
Go to Google Cloud Console, and click on Select a Project to create New Project, then go to the Credentials tab on the sidebar, and click on Create Credentials and select OAuth 2.0 Client ID. And configure the OAuth consent screen by filling in the required fields.
Select Web Application in Under Application Type. And add Project URL or Callback URL to it. Click on Create. Note down your Client ID and Client Secret.
Step 2: Install Google PHP Client Library
Run the following composer command to install Google API PHP Client Library:
composer require google/apiclient
Step 3: Configure CodeIgniter
Create Google.php
configuration file in app/Config/
, and add YOUR_CLIENT_ID
and YOUR_CLIENT_SECRET
in it:
<?php
namespace App\Config;
use CodeIgniter\Config\BaseConfig;
class Google extends BaseConfig
{
public $clientId = 'YOUR_CLIENT_ID';
public $clientSecret = 'YOUR_CLIENT_SECRET';
public $redirectUri = 'http://localhost/google-login/callback';
}
Step 4: Create Google Auth Controller
Go to app/Controllers/
controller directory, and create GoogleLogin.php
file to handle google authentication:
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
use Google\Client as GoogleClient;
use Google\Service\Oauth2 as GoogleOauth2;
class GoogleLogin extends Controller
{
public function index()
{
return view('login');
}
public function googleAuth()
{
$config = config('Google');
$client = new GoogleClient();
$client->setClientId($config->clientId);
$client->setClientSecret($config->clientSecret);
$client->setRedirectUri($config->redirectUri);
$client->addScope('email');
$client->addScope('profile');
$authUrl = $client->createAuthUrl();
return redirect()->to($authUrl);
}
public function callback()
{
$config = config('Google');
$client = new GoogleClient();
$client->setClientId($config->clientId);
$client->setClientSecret($config->clientSecret);
$client->setRedirectUri($config->redirectUri);
$code = $this->request->getVar('code');
$client->fetchAccessTokenWithAuthCode($code);
$oauth2 = new GoogleOauth2($client);
$userInfo = $oauth2->userinfo->get();
// Display user info
echo 'Name: ' . $userInfo->name . '<br>';
echo 'Email: ' . $userInfo->email . '<br>';
echo 'Profile Picture: <img src="' . $userInfo->picture . '"><br>';
}
}
Step 5: Create View
Go to app/Views/
directory and create login.php
file that show google login button:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Google Login in CodeIgniter 4 - Itcodstuff.com</title>
</head>
<body>
<h1>Login Page</h1>
<a href="<?= site_url('google-login') ?>">Login with Google</a>
</body>
</html>
Step 6: Define Routes
Edit app/Config/Routes.php
file and add google authentication routes in it:
$routes->get('login', 'GoogleLogin::index');
$routes->get('google-login', 'GoogleLogin::googleAuth');
$routes->get('google-login/callback', 'GoogleLogin::callback');
Step 7: Test Application
Run the following command to start application server:
php spark serve
Now open browser and type url http://localhost:8080/login
in it to test google login application.