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

Google oauth 2.0 in CodeIgniter

 
  • 0
code

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>

Leave a Reply

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