Google OAuth API is the simple and powerful way to integrate login system in the website. Google OAuth Login API let you allow the user to sign in the web application using their Google account without registration. The main advantage of Google login is the user can log in to the web application with their existing Google account without register account on the website. Nowadays almost all web users have a Google account, Sign-in with Google helps to increase the user’s subscription on your website.

Step1:-
Create Google API Console Project
-> Go to Google API Console.
-> Create project,Auth credentials for the project.

Step2:-
-> Download the latest version of codeigniter and extract into your root folder.

Step3:-
Create a google_config.php in /config folder, place below code.

defined('BASEPATH') OR exit('No direct script access allowed'); 
/* | To get API details you have to create a Google Project 
| at Google API Console (https://console.developers.google.com) 
| client_id string Your Google API Client ID.
| client_secret string Your Google API Client secret. 
| redirect_uri string URL to redirect back to after login. 
| application_name string Your Google application name. 
| api_key string Developer key. 
| scopes string Specify scopes */
$config['google']['client_id'] = 'Google_API_Client_ID';
$config['google']['client_secret'] = 'Google_API_Client_Secret';
$config['google']['redirect_uri'] = 'https://example.com/project_folder_name/user_authentication/';
$config['google']['application_name'] = 'Login to example.com';
$config['google']['api_key'] = '';
$config['google']['scopes'] = array(); 

Step4:-

Create Controller Auth.php In yor controllers/ folder.

if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Auth extends CI_Controller { function __construct() { parent::__construct(); $this->load->library('google');<br ?--> }
public function index(){
$data['google_login_url']=$this->google->get_login_url();
$this->load->view('home',$data);
}
public function oauth2callback(){
$google_data=$this->google->validate();
$session_data=array(
'name'=>$google_data['name'],
'email'=>$google_data['email'],
'source'=>'google',
'profile_pic'=>$google_data['profile_pic'],
'link'=>$google_data['link'],
'sess_logged_in'=>1
);
$this->session->set_userdata($session_data);
redirect(base_url());
}
public function logout(){
session_destroy();
unset($_SESSION['access_token']);
$session_data=array('sess_logged_in'=>0);
$this->session->set_userdata($session_data);
redirect(base_url());
}
}

Step5:-
Create A view home.php file in view forlder.

<html>
<head>
<title>Google authentication library for codeigniter</title>
<!-- Compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.96.1/css/materialize.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">

<!-- Compiled and minified JavaScript -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.96.1/js/materialize.min.js"></script>
</head>
<body>
<div align="center">
<h2>Google authentication library for Codeigniter</h2>
</div>
<div class="container">
<div class="row">
<div class="col s12 m6 offset-m3 l6 offset-l3">

<?php
if($this->session->userdata('sess_logged_in')==0){?>
<a href="<?=$google_login_url?>"class="waves-effect waves-light btn red"><i class="fa fa-google left"></i>Google login</a>
<?php }else{?>
<a href="<?=base_url()?>auth/logout" class="waves-effect waves-light btn red"><i class="fa fa-google left"></i>Google logout</a>
<?php }
?>

</div>
</div>
<div class="row">

<?php if(isset($_SESSION['name'])){?>
<div class="col s12 m6 l4 offset-l3 " >
<div class="card ">
<div class="card-image waves-effect waves-block waves-light">
<img class="activator" src="<?=$_SESSION['profile_pic']?>">
</div>
<div class="card-content">
<span class="card-title activator grey-text text-darken-4"> <i class="material-icons"><?=$_SESSION['name']?></i></span>
</div>
<div class="card-reveal">
<span class="card-title grey-text text-darken-4"><?=$_SESSION['name']?><i class="material-icons right">close</i></span>
<p>Email Id: <?=$_SESSION['email']?></p>
</div>
</div>
</div>
<?php }?>
</div>
</div>
</body>
</html>

————————————————————-

Google Old Article

Google login in php.It is very easy at the same time,if somebody needs Google login in codeigniter websites.Just Follow the Steps you can easily login with Google in codeigniter.

 

Step 1:
Download the api Oauth2 Service package.
Note: If you are unable to find the package anywhere,please mail to me, I will send it to your mail id.

 

Step 2:
Download codeigniter latest version.

 

Step 3:
Extract and put the you are root folder.

 

Step 4:
Copy “src” folder from “apiOauth2Service” package and paste into codeigniter third_party folder.

 

Step 5:
Create config file into config folder name ‘google.php’.
Just write the below code

$config['google']['application_name'] = '';
$config['google']['client_id'] = '’;
$config['google']['client_secret'] = '’;
$config['google']['redirect_uri'] = 'http://www.domain.com/google/googleLoginSubmit';
$config['google']['api_key'] =’’;

Step 6:
Here go to Google api console and create an app and copy the client_id,client_secret,redirect_uri,api_key
Paste it into config file.Completed the main configuration part in codeigniter

 

Step 7:
Create one controller name google.php
Just write down the below code

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Google extends CI_Controller {
public function __construct() {
parent::__construct();
$this->config->load('google');
require APPPATH .'third_party/src/apiClient.php';
require APPPATH .'third_party/src/contrib/apiOauth2Service.php';
$cache_path = $this->config->item('cache_path');
$GLOBALS['apiConfig']['ioFileCache_directory'] = ($cache_path == '') ? APPPATH .'cache/' : $cache_path;
$this->client = new apiClient();
$this->client->setApplicationName($this->config->item('application_name', 'google'));
$this->client->setClientId($this->config->item('client_id', 'google'));
$this->client->setClientSecret($this->config->item('client_secret', 'google'));
$this->client->setRedirectUri($this->config->item('redirect_uri', 'google'));
$this->client->setDeveloperKey($this->config->item('api_key', 'google'));
$this->oauth2 = new apiOauth2Service($this->client);
}
/**
* Welcome index
*
* @access     public
*/
public function index()
{
$data['auth'] = $this->client->createAuthUrl();
$this->load->view('google', $data);
}
public function googleLoginSubmit' (){
$this->input->get('code');
$this->client->authenticate();
$data1=$this->client->getAccessToken();
$data['user'] = $this->oauth2->userinfo->get();
echo "<pre>"; print_r($data);
//$this->load->view('google', $data);
}
}
?>

Step 8:
Create file in views name as google.php
Just write the below code

<?php
echo "Login with google";
?>
<br />
<a href="<?php echo $auth;?>">Google Login</a>

That is it!!! You can now login with Google in Codeigniter.