
- Views: 5.4K
- Category: Codeigniter
- Published at: 13 Aug, 2017
- Updated at: 18 Aug, 2023
Login with google plus in codeigniter
How to log in with google plus in Codeigniter?
Why do you constantly design for login to your client/users on your system/site..? Why do they fill your long/short form and validate emails..? Nowadays, every person is busy, and they don't have time to fill out their forms for a single comment. It's a lengthy procedure to create an account on your application or site, so what is the solution..?
You can create a system so your user/client can log in with a single click; how it's possible..? You can generate a Login with a social network system, so your user/client easily log in with a single click.
Today, I will show you how to create a system login with google plus in CodeIgniter.
Requirements:
Google+ API
Codeigniter3
log in with the Google+ library in Codeigniter.
You can download all setups from Github.
Step1: Create a Project on console.developers.google.com
Step 2: Go to your application/config/autoload.php file and set these two libraries to autoload (session, google plus) because we will use them in our controller.
You can check other files and folders.
application/libraries/Googleplus.php
application/third_party/google-login-API
Step 3: Go to application/config/ and create a file named googleplus.php and write this code inside your file.
Step 4: Create a controller named LoginWithGooglePlus.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$config['googleplus']['application_name'] = ''";
$config['googleplus']['client_id'] = 'Your Client Id';
$config['googleplus']['client_secret'] = 'Your Client Secret';
$config['googleplus']['redirect_uri'] = 'Your redirect URL';
$config['googleplus']['api_key'] = '';
$config['googleplus']['scopes'] = array();
Step 5: Write an index method in your controller.
public function index(){
if(isset($_GET['code']))//checking if the data is comming from API in code variable
{
$this->googleplus->getAuthenticate(); //generating token
$this->session->set_userdata('login',true);//creating session
$this->session->set_userdata('userProfile',$this->googleplus->getUserInfo());//creating session
redirect('LoginWithGooglePlus/profile');//when we set the session we redirect the user to profile view
}
$data['loginURL'] = $this->googleplus->loginURL();//getting URL from calling API
$this->load->view('login',$data);
}
Using this function $this->googleplus->getUserInfo() we are getting a user profile from gogole+ API.
Step 6: Create a file named login.php in your view folder and write these lines in this file.
<h1>Login with google PLus in codeigniter</h1>
<a href="<?php echo $loginURL;?>">
Login With googlePlus
</a>
Step 7: Create a function/method named profile and write this code in this method.
public function profile(){
if($this->session->userdata('login') == true)//checking session
{
$data['profileData'] = $this->session->userdata('userProfile'); //getting data from session ans storing.
$this->load->view('profile',$data); //loading a view.
}
else
{
redirect('');
}
}
Step 8: Create a file named profile.php in your view folder and write these lines.
Codeigniter Login with google plus <?php var_dump($profileData); //userdata ?> ID: <?php echo $profileData['id']; ?> Email: <?php echo $profileData['email']; ?> Verified Email: <?php echo $profileData['verified_email']; ?> Name: <?php echo $profileData['name']; ?> Profile Picture: <img src="<?php echo $profileData['picture']; ?>"> <a href="<?php site_url('LoginWithGooglePlus/logout')?>;">Logout</a><!--This is the logout button->
Step 9: Create a function/method named log out and write this code.
public function logout(){
$this->session->sess_destroy(); //destorying codeignier's session
$this->googleplus->revokeToken(); //destorying google+ token
redirect('');
}
In your controller, here is the complete code.
<?php
class LoginWithGooglePlus extends CI_Controller{
public function index(){
if(isset($_GET['code']))
{
$this->googleplus->getAuthenticate();
$this->session->set_userdata('login',true);
$this->session->set_userdata('userProfile',$this->googleplus->getUserInfo());
redirect('LoginWithGooglePlus/profile');
}
$data['loginURL'] = $this->googleplus->loginURL();
$this->load->view('login',$data);
}
public function profile(){
if($this->session->userdata('login') == true)
{
$data['profileData'] = $this->session->userdata('userProfile');
$this->load->view('profile',$data);
}
else
{
redirect('');
}
}
public function logout(){
public function logout(){
$this->session->sess_destroy();
$this->googleplus->revokeToken();
redirect('');
}
}//class ends here
https://www.youtube.com/watch?v=3wxkIM5Ypxg
- Tag
0 Comment(s)