We are Designers, We are Developers,We are Thinkers,
We are Strategists

Google oauth 2.0 in CodeIgniter

  • 0

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.

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

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

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(); 


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(){
public function oauth2callback(){
public function logout(){

Create A view home.php file in view forlder.

<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>
<div align="center">
<h2>Google authentication library for Codeigniter</h2>
<div class="container">
<div class="row">
<div class="col s12 m6 offset-m3 l6 offset-l3">

<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 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 class="card-content">
<span class="card-title activator grey-text text-darken-4"> <i class="material-icons"><?=$_SESSION['name']?></i></span>
<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>
<?php }?>

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.