Skip to main content

How to Create "Google Login" in Codeigniter?

Google Login Integration in Codeigniter






Paste in Controller




$uuser_id = '';
include_once(FCPATH.'googlelogin-library/vendor/autoload.php');

$google_client = new Google_Client();

$google_client->setClientId('975330128814-il47gqr1l99uleblged7altinhpm99g3.apps.googleusercontent.com'); //Define your ClientID

$google_client->setClientSecret('c-6xwtqoC4DRXV_blQ9QB6Lu'); //Define your Client Secret Key

$google_client->setRedirectUri(base_url().'login'); //Define your Redirect Uri
$google_client->addScope('email');

$google_client->addScope('profile');
 
//echo '<pre>'; 
// print_r($google_client);
//echo $_GET["code"];
  if(isset($_GET["code"]))
  {


   $token = $google_client->fetchAccessTokenWithAuthCode($_GET["code"]);

   if(!isset($token["error"]))
   {
$google_client->setAccessToken($token['access_token']);

$this->session->set_userdata('access_token', $token['access_token']);

$google_service = new Google_Service_Oauth2($google_client);

$data = $google_service->userinfo->get();

$current_datetime = date('Y-m-d H:i:s');

if(count($this->User_Model->check_google_account_exists($data['id']))>0)
{
//update data 
$user_data = array(
  'name' => $data['given_name'],
  'lastname'  => $data['family_name'],
  'email' => $data['email'],
);

// $this->User_Model->update_google($user_data, $data['id']);
$googleresult = $this->User_Model-> google_login($data['email'], $data['id']);
$uuser_id = $googleresult[0]->id;
}
else
{
if(count($this->User_Model->get_email_data($data['email']))>0){
$this->session->set_flashdata('login_failed', 'This email is already registered.');
redirect('login');
}
else{
$user_data = array(
  'google_id' => $data['id'],
  'name'  => $data['given_name'],
  'lastname'   => $data['family_name'],
  'email'  => $data['email'],
  'role'  => 'employee'

);
$uuser_id = $this->User_Model->googleregistation($user_data);
}



$user_detail = $this->User_Model->get_single_user($uuser_id);
//echo count($user_detail);
//print_r($user_detail);
if (count($user_detail) > 0) {
//Create Session
$user_data = array(
'full_user_detail' => $user_detail,
'user_id' => $user_detail[0]->id,
'email' => $user_detail[0]->email,
'role' => $user_detail[0]->role,
'username' => $user_detail[0]->name.' '.$user_detail[0]->lastname,
'is_user_login' => true
);
 
$set_session = $this->session->set_userdata($user_data);
$this->session->set_flashdata('user_loggedin', 'You are now logged in.');
redirect('users/dashboard'); 
}
else{
$this->session->set_flashdata('login_failed', 'Login is invalid.');
redirect('login');
}
   }
  } 
  
  $login_button = '';
  if(!$this->session->userdata('is_user_login'))
  {
   $login_button = $google_client->createAuthUrl();
//echo $login_button;
//   die();
   $data['login_button'] = $login_button;
  }

$this->load->view('users/login', $data);



Model  in Controller


public function get_single_user($id){
$this->db->select("*"); 
$this->db->from('users');
$this->db->where('id', $id);
$query = $this->db->get();
return $query->result();
}


/*========================================Google Account=========================================*/

public function update_google($data,$user_id){
$this->db->where('google_id', $user_id);
return $this->db->update('users', $data);
}


// Check email exists
public function check_google_account_exists($google_id){
$this->db->select("*"); 
$this->db->from('users');
$this->db->where('google_id', $google_id);
$query = $this->db->get();
return $query->result();
}
public function googleregistation($userdata){
$this->db->insert('users', $userdata);
return $insertId = $this->db->insert_id();
}
public function google_login($email, $google_id){
//Validate
$this->db->select("*"); 
$this->db->from('users');
$this->db->where('email', $email);
$this->db->where('google_id', $google_id);
//$this->db->where("(role='employee' OR role='employer')", NULL, FALSE);
$query = $this->db->get();
return $query->result();
}


View in Controller

<a href="<?php if(!empty($login_button)){ echo $login_button; } ?>" >Login with Google</a>

Comments

Popular posts from this blog

How to check date format in PHP?

Check date format in PHP function isCorrectDateFromat($date){     if(!empty($date)){         $dateString = $date; // Replace this with your date string         $format = "Y-m-d"; // Replace this with your expected date format         $dateTime = DateTime::createFromFormat($format, $dateString);         if ($dateTime === false) { /*             echo "The date is not in the correct format."; */         } else {             $errors = DateTime::getLastErrors();             if (empty($errors)) { /*                 echo "The date is in the correct format."; */                 return true;             } else { /*                 echo "...