Microsoft Login / Signup in codeigniter php framework with mysql | 2my4edge

10 August 2017

Microsoft Login / Signup in codeigniter php framework with mysql

Microsoft login / Signup in codeigniter php framework with mysql database, it's microsoft login script, which makes our registration easy and simple. in this microsoft login we are fetching email so you can check existing user in database with mysql code. This microsoft login is very simple way, we just want to create microsoft app and one simple php function for this, lets see the step by step process for this.

Microsoft oauth login in php framework codeigniter

DOWNLOAD           LIVE DEMO

FLOW DIAGRAM OF MICROSOFT DIAGRAM

Microsoft oauth login in php framework codeigniter flow diagram

CONFIG AND INSTALLATION VIDEO


STEP 1 : Create a microsoft app by using this link for app id and secret key id. CREATE MICROSOFT LOGIN APP

STEP 2 : Then configure the redirect url. Site url is not mandatory. so you can leave that.

STEP 3 : After succesfully created Microsoft app, configure your codeigniter files.

STEP 4 : Config all important files like autoload, config, and database files.

STEP 5 : Add Microsoft.php file into application -> config folder.

STEP 6 : Change app id, secret key id and redirect url in microsoft.php file.

STEP 7 : Add Microsoft library files into application -> libraries folder.

STEP 8 : CONTROLLER

<?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('microsoft');
$this->load->config('microsoft');
$this->load->helper('url');
$this->load->model('welcome_model','welcome'); 
}


public function index()
{

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

}






public function microsoft_login() {

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

//Include the linkedin api php libraries
include_once APPPATH."libraries/Microsoft/http.php";
include_once APPPATH."libraries/Microsoft/oauth_client.php";

$microsoft_application_id = $ci->config->item('microsoft_application_id');
$microsoft_secret_id = $ci->config->item('microsoft_secret_id');
$microsoft_redirect_url = $ci->config->item('microsoft_redirect_url');
$microsoft_scope = $ci->config->item('microsoft_scope');


if((isset($_REQUEST["oauth_init"]) && $_REQUEST["oauth_init"] == '1' ) || 
(isset($_GET["oauth_init"]) && $_GET["oauth_init"] == '1' ) || 
(isset($_REQUEST['code'])) || (isset($_GET['code'])) ){

$client = new oauth_client_class;
$client->server = 'Microsoft';
$client->client_id = $microsoft_application_id;
$client->client_secret = $microsoft_secret_id;
$client->redirect_uri = base_url().$microsoft_redirect_url;
$client->scope = $microsoft_scope;
$client->debug = false;
$client->debug_http = true;
$application_line = __LINE__;

$client->scope = 'wl.basic wl.emails wl.birthday';
if(($success = $client->Initialize()))
{
if(($success = $client->Process()))
{
if(strlen($client->authorization_error))
{
$client->error = $client->authorization_error;
$success = false;
}
elseif(strlen($client->access_token))
{
$success = $client->CallAPI(
'https://apis.live.net/v5.0/me',
'GET', array(), array('FailOnAccessError'=>true), $user);
}
}
$success = $client->Finalize($success);
}

if($client->exit) exit;

$checkemail = $this->db->query('select id,email 
    from users where email = "'.$user->emails->preferred.'"');
$emailresult = $checkemail->result_array();
if($emailresult[0]['email'] != $user->emails->preferred){
$user_information  = array(
'name'              => $user->name,
'first_name'        => $user->first_name,
'last_name'         => $user->last_name,
'email'             => $user->emails->preferred,
'gender'            => $user->gender,
'source'            => 'Microsoft',
'source_id'         => $user->id,
'profilepicture'    => 'https://cid-'.$user->id.'.users.storage.
                        live.com/users/0x'.$user->id.'/myprofile/
                        expressionprofile/profilephoto:Win8Static,
                        UserTileMedium,UserTileStatic/MeControlXXLUserTile?ck=1&ex=1',
);
$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->emails->preferred){
$update_id = array('source_id'  => $user->id, 'source' => 'Microsoft');
$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 Microsoft 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 9 : MODEL
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 10 : VIEWS
1. profile.php
2. login.php / welcome_message.php

PROFILE.PHP has to view the user data in that page.

LOGIN.PHP / WELCOME_MESSAGE.PHP file is to give login url link.
<a href="<?php echo $this->microsoft->loginURL(); ?>">
<span class="fa fa-windows"></span> Sign in with Microsoft
</a>

Hopr you like this post, and will be very useful to you make microsoft login step by step process. for complete sample demo. see the video demo in the given link.

1 comment:

  1. I am very happy to see this type article. It is very useful and exciting. The best blessings of this article are giving properly idea to each and every reader and also it's giving correct impressions. Looking forward to new article.

    ReplyDelete

^