Github login / Signup using codeigniter php framework with mysql | 2my4edge

30 August 2017

Github login / Signup using codeigniter php framework with mysql

Github login / Signup using codeigniter php framework with mysql database, Github login script is used to make your site regisration very simple and easy, in this script we are taking email id also, so you can easily check the existing users in mysql database. for that we need to created github app for oauth login. first we need to create that to futher process. let see the step by step process here.

Github login or signup oauth


FLOW DIAGRAM OF GITHUB LOGIN
Github login or signup oauth flow

CONFIG AND INSTALATION VIDEO DEMO



STEP 1 : Create Github oauth login app to get client id and client secret key. Github apps

STEP 2 : For reference see the demo video. set callback url also.

STEP 3 : Configure your codeigniter files, configure config files like autoload, config and database etc.

STEP 4 : Add github.php file into application -> config folder.

STEP 5 : Change client id, client secret key, url, and app name.

STEP 6 : Add github library files to application -> libraries folder.

STEP 7 : CONTROLLER
Welcome.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
error_reporting(0);
class Welcome extends CI_Controller {

public function __construct()
{
parent::__construct();
$this->load->library('github');
$this->load->config('github');
$this->load->helper('url');
$this->load->model('welcome_model','welcome'); 
}


public function index()
{

$this->load->view('login');

}





public function github_login() {

$user = array();
$ci = get_instance(); // CI_Loader instance
$ci->load->config('github');

include_once APPPATH."libraries/Github/apiGithub.php";

$config['github_client_id']         = $ci->config->item('github_client_id');
$config['github_client_secret']     = $ci->config->item('github_client_secret');
$config['github_redirect_url']      = $ci->config->item('github_redirect_url');
$config['github_app_name']          = $ci->config->item('github_app_name');


if($_SERVER['REQUEST_METHOD'] == 'GET') 
{
if(isset($_GET['code']))
{
$git = new apiGithub($config);
$git->getUserDetails();
$user=$git->getAllUserDetails();
}
$checkemail = $this->db->query('select id,email 
                from users 
                where email = "'.$user->email.'"');
$emailresult = $checkemail->result_array();
if($emailresult[0]['email'] != $user->email){
$names =  explode(" ", $user->name);
$user_information  = array(
'name'              => $user->name,
'first_name'        => $names[0],
'last_name'         => $names[1],
'email'             => $user->email,
'gender'            => '',
'source'            => 'Github',
'source_id'         => $user->id,
'profilepicture'    => $user->avatar_url,
);
$this->welcome->insert_user($user_information);
$insert_id = $this->db->insert_id();
$fetchuser = $this->db->query('select * 
                        from users 
                        where id = "'.$insert_id.'"');
$userdata = $fetchuser->result_array();
$this->session->set_userdata ( 'user_id', $userdata[0]['id'] );
$this->session->set_userdata ( 'user_name', $userdata[0]['name'] );
$this->session->set_userdata ( 'user_email', $userdata[0]['email'] );
$this->session->set_userdata ( 'user_gender', $userdata[0]['gender'] );
$this->session->set_userdata ( 'user_source', $userdata[0]['source'] );
$this->session->set_userdata ( 'user_source_id', $userdata[0]['source_id'] );
}else if($emailresult[0]['email'] == $user->email){
$update_id = array('source_id'  => $user->id, 
                    'source' => 'Github', 
                    'profilepicture' => $user->avatar_url);
$this->db->where('id', $emailresult[0]['id']);
$this->db->update('users', $update_id);
$fetchuser = $this->db->query('select * 
                    from users 
                    where id = "'.$emailresult[0]['id'].'"');
$userdata = $fetchuser->result_array();
$this->session->set_userdata ( 'user_id', $userdata[0]['id'] );
$this->session->set_userdata ( 'user_name', $userdata[0]['name'] );
$this->session->set_userdata ( 'user_email', $userdata[0]['email'] );
$this->session->set_userdata ( 'user_gender', $userdata[0]['gender'] );
$this->session->set_userdata ( 'user_source', $userdata[0]['source'] );
$this->session->set_userdata ( 'user_source_id', $userdata[0]['source_id'] );
}
$data['userprofile'] = $this->session->userdata();
redirect('welcome/profile', $data);
}else{
echo 'We are unable fetch your Github information.'; exit;
}



}




public function profile()
{
if($this->session->userdata('user_id') != ''){
$this->data['userprofile']= $this->welcome->fetch_user();
$this->load->view('profile', $this->data, FALSE);
}else{
redirect('welcome', $data);
}
}




public function logout()
{
$this->session->sess_destroy();
redirect(base_url(), redirect);
}
}


STEP 8 : MODELS
Welcome_model.php
<?php
class Welcome_model extends CI_Model
{
public function __construct()
{
parent::__construct();
}


public function insert_user($data){
$this->db->insert('users', $data); 
return TRUE;
}

public function fetch_user(){
$query=$this->db->query("SELECT *
 FROM users
 WHERE id = '".$this->session->userdata('user_id')."' ");
//return $query->result();
return $query->row(0);
}

}


STEP 9 : 
1. profile.php
2. login.php / welcome_message.php

PROFILE.PHP  is to view the user information.

LOGIN.PHP is for to give the link

LOGIN.PHP / WELCOME_MESSAGE.PHP PAGE
<a href="<?php echo $this->github->loginURL(); ?>">
<span class="fa fa-github"></span> Sign in with Github
</a>

Hope this post is really helpful to you to make github social oauth login. to see complete step by step process in video.  click on the link for video demo.

4 comments:

  1. This post provides more useful information. Really glad to read this article and I will refer this site to my friends. The way of writing is good and interesting. I am thankful to you for sharing this useful article. I have thoroughly enjoyed reading your article.

    ReplyDelete
  2. Nice and perfect codes i love the programmation i use it always on my work good job my friend.

    ReplyDelete
  3. I couldn't download the files from database. Is anybody checked this?

    ReplyDelete

^