Google authentication in codeigniter 3


Today I will show you the easiest way to integrate google API PHP client version 2 in your CodeIgniter project. You need to create an app in google API console before using google API. You can create an app for free easily by visiting following link

  1. Search for google plus API there and follow the link.
  2. Then create a new project.
  3. After that go to credentials session (right side) and then go to OAuth consent screen and fill your product name and save.
  4. Then go to credentials click on create credentials and choose OAuth client ID.
  5. Application type web application
  6. Viola, you get your client id and client secret. We use this key for authentication purpose.

Now we need to install google API client library for PHP using composer.

composer require google/apiclient:^2.0

If you don't know how to add composer in your CodeIgniter project visit my tutorial here.

Now coding parts.

In login view page

<p>
    <a href="<?php echo base_url();?>login/glogin">Login with Google</a>
</p>	

In glogin function in login controller

function glogin()
    {
            // Fill CLIENT ID, CLIENT SECRET ID, REDIRECT URI from Google Developer Console
        $client_id = 'your client id';
        $client_secret = 'your secret';
        $redirect_uri = base_url('login/gcallback');;

        //Create Client Request to access Google API
        $client = new Google_Client();
        $client->setApplicationName("Yourappname");
        $client->setClientId($client_id);
        $client->setClientSecret($client_secret);
        $client->setRedirectUri($redirect_uri);
        $client->addScope("email");
        $client->addScope("profile");

        //Send Client Request
        $objOAuthService = new Google_Service_Oauth2($client);
        
        $authUrl = $client->createAuthUrl();
        
        header('Location: '.$authUrl);
    }

Above function will redirect you to google authentication section and then return you to gcallback function in login controller. The code for gcallback is given below.

function gcallback()
    {
            // Fill CLIENT ID, CLIENT SECRET ID, REDIRECT URI from Google Developer Console
     $client_id = 'your client id';
     $client_secret = 'your client secret';
     $redirect_uri = base_url('login/gcallback');

    //Create Client Request to access Google API
    $client = new Google_Client();
    $client->setApplicationName("Yourappname");
    $client->setClientId($client_id);
    $client->setClientSecret($client_secret);
    $client->setRedirectUri($redirect_uri);
    $client->addScope("email");
    $client->addScope("profile");

    //Send Client Request
    $service = new Google_Service_Oauth2($client);

    $client->authenticate($_GET['code']);
    $_SESSION['access_token'] = $client->getAccessToken();
    
    // User information retrieval starts..............................

    $user = $service->userinfo->get(); //get user info 

    echo "</br> User ID :".$user->id; 
    echo "</br> User Name :".$user->name;
    echo "</br> Gender :".$user->gender;
    echo "</br> User Email :".$user->email;
    echo "</br> User Link :".$user->link;    
    echo "</br><img src='$user->picture' height='200' width='200' > ";
       
    }

The above function will fetch basic user information from the facebook and displays it.The sample output is as below.

Google authentication by php example img

Now for making your own google authentication system in your project by steps like storing the user information in the table if new and user session to store necessary data for your authentication and at last redirect to the appropriate page. I just skipped that steps because it mainly depends on your authentication system codes and I don't want to mess it. If you have any doubts or suggestion then please comment below.


Web development
22nd Jul 2018 02:00:47 AM
PHP CodeIgniter
7995

ShareurCodes

ShareurCodes is a code sharing site for programmers to learn, share their knowledge with younger generation.