Google Authentication: Authenticate with Gmail in Codeigniter

Google Authentication: Authenticate with Gmail in Codeigniter

How to Authenticate with Google ? In this post I would like to show how we can get authenticate with gmail in codeigniter. Google Oauth login API allow to authenticate user into web applications by using their gmail account. Follow me i will show you step by step how we can achieve this.

Get API and Secret Key:

Before integrating gmail authentication into our application, we need to create some Google oauth client credentials.To create oauth client credential, Simple go to google developer console and create all oauth credentials.

Create Gmail API

Installation of Google Oauth library in Codeigniter:

Once you created credentials in google console, then after you need to install google oauth library using composer in your codeigniter app. you can get it easily installed.Go to your root folder of the codeigniter application and run Below command.

composer require google/apiclient:^2.0

Gmail Authentication With Codeigniter:

1.) Create a controller called GoogleLoginController.php inside application/controllers directory and put the code as i am showing below.

GoogleLoginController.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class GoogleLoginController extends CI_Controller {
public function __construct()
{
parent::__construct();
}
Public function index()
{
$client_id = 'YOUR_CLIENT_ID'; /* Client ID */
$client_secret = 'YOUR_CLIENT_SECRET_ID';/* Client secret ID */
$redirect_uri = base_url('GoogleLoginController/googlecallback');;
/*Create Client Request to access Google Oauth API*/
$client = new Google_Client();
$client->setApplicationName("Yourappname");
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("email");
$client->addScope("profile");
//Send Client Request
$objOAuthService = new Google_Service_Oauth2($client);
$authUrl = $client->createAuthUrl();
header('Location: '.$authUrl);
}
function googlecallback()
{
$client_id = 'YOUR_CLIENT_ID';
$client_secret = 'YOUR_CLIENT_SECRET_ID';
$redirect_uri = base_url('GoogleLoginController/googlecallback');
$client = new Google_Client();
$client->setApplicationName("Yourappname");
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("email");
$client->addScope("profile");
$service = new Google_Service_Oauth2($client);
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
/* User informations retrieving */
$user = $service->userinfo->get(); //get user info
echo "User Informations:";
echo "</br> User ID :".$user->id;
echo "</br> User Name :".$user->name;
echo "</br> Gender :".$user->gender;
echo "</br> User Email :".$user->email;
echo "</br> User Link :".$user->link;
echo "</br><img src='$user->picture' height='200' width='200' > ";
}
}

This example simply authenticate with google and we get all user data from google. You can use this for your application’s login system by checking user if new and storing necessary data into your database.Thank you for reading this post. we hope you like this Post, Please feel free to comment below, your suggestion. if you face any issue with this code let us know. We’d love to help!