.

.

Facebook Login in magento programattically

<?php
//header("Content-Type: application/json");
require_once 'app/Mage.php';
//facebook
require_once 'app/code/community/Inchoo/SocialConnect/controllers/FacebookController.php';
require_once 'app/code/community/Inchoo/SocialConnect/Helper/Facebook.php';
require_once 'app/code/community/Inchoo/SocialConnect/Model/Facebook/Client.php';
require_once 'app/code/community/Inchoo/SocialConnect/Model/Facebook/Userinfo.php';

Mage::app('default');  
Mage::getSingleton("core/session", array("name" => "frontend"));
$data=array();

if($_REQUEST['facebookId']=='')
{
    $data['responseCode']='0';
    $data['msg']='Please Enter Facebook Id';
    echo json_encode($data);
return;
}
else
{
    $facebookId=$_REQUEST['facebookId'];  
}

if($_REQUEST['firstName']=='')
{
    $data['responseCode']='0';
    $data['msg']='Please Enter FirstName';
    echo json_encode($data);
return;
    }
else
{
$firstName=$_REQUEST['firstName'];  
}

if($_REQUEST['lastName']=='')
{
    $data['responseCode']='0';
    $data['msg']='Please Enter LastName';
    echo json_encode($data);
return;
    }
    else
    {
      
$lastName=$_REQUEST['lastName'];          
        }


if($_REQUEST['email']=='')
{
    $data['responseCode']='0';
    $data['msg']='Please Enter Email';
    echo json_encode($data);
return;
    }
    else
    {
        $email=$_REQUEST['email'];
      
        }


if($_REQUEST['token']=='')
{
    $data['responseCode']='0';
    $data['msg']='Please Enter Token';
    echo json_encode($data);
return;
}
else
{
    $token=$_REQUEST['token'];
}

if($firstName!='' && $lastName!='' && $email!=''  && $facebookId!='')
{
    // start here facebook code
  
               // Facebook API green light - proceed
            $client = Mage::getSingleton('inchoo_socialconnect/facebook_client');

          
          //$token = $client->getAccessToken();
          $token=$token;

            $customersByFacebookId = Mage::helper('inchoo_socialconnect/facebook')->getCustomersByFacebookId($facebookId);
            //print_r($customersByFacebookId);
      $customersByFacebookId->count();
            if(Mage::getSingleton('customer/session')->isLoggedIn()) {
                // Logged in user
                if($customersByFacebookId->count()) {
                    // Facebook account already connected to other account - deny
                    Mage::getSingleton('core/session')
                        ->addNotice('Your Facebook account is already connected to one of our store accounts.');
                        $data['responseCode']='1';
                        $data['msg']='Your Facebook account is already connected to one of our store accounts';
                        echo json_encode($data);
                    return;
                }

                // Connect from account dashboard - attach
                $customer = Mage::getSingleton('customer/session')->getCustomer();

                Mage::helper('inchoo_socialconnect/facebook')->connectByFacebookId($customer,$facebookId,$token);

                Mage::getSingleton('core/session')->addSuccess('Your Facebook account is now connected to your store accout. You can now login using our Facebook Connect button or using store account credentials you will receive to your email address.');
              
$data['responseCode']='1';
                        $data['msg']='Your Facebook account is now connected to your store accout. You can now login using our Facebook Connect button or using store account credentials you will receive to your email address';

echo json_encode($data);
                return;
            }

            if($customersByFacebookId->count()) {
                // Existing connected user - login
                $customer = $customersByFacebookId->getFirstItem();

                Mage::helper('inchoo_socialconnect/facebook')->loginByCustomer($customer);

                Mage::getSingleton('core/session')
                    ->addSuccess('You have successfully logged in using your Facebook account.');

$data['responseCode']='1';
                        $data['msg']='You have successfully logged in using your Facebook account';

echo json_encode($data);

                return;
            }

            $customersByEmail = Mage::helper('inchoo_socialconnect/facebook')->getCustomersByEmail($email);

            if($customersByEmail->count()) {              
                // Email account already exists - attach, login
                $customer = $customersByEmail->getFirstItem();
              
                Mage::helper('inchoo_socialconnect/facebook')->connectByFacebookId($customer,$facebookId,$token);

                Mage::getSingleton('core/session')->addSuccess('We have discovered you already have an account at our store. Your Facebook account is now connected to your store account.');
                $data['responseCode']='1';
                $data['msg']='We have discovered you already have an account at our store. Your Facebook account is now connected to your store account';
                echo json_encode($data);

                return;
            }

            // New connection - create, attach, login
            if(empty($firstName)) {
                throw new Exception('Sorry, could not retrieve your Facebook first name. Please try again.');
                $data['responseCode']='0';
                $data['msg']='Sorry, could not retrieve your Facebook first name. Please try again';
                echo json_encode($data);
                return;
              
            }

            if(empty($lastName)) {
                throw new Exception('Sorry, could not retrieve your Facebook last name. Please try again.');
                $data['responseCode']='0';
                $data['msg']='Sorry, could not retrieve your Facebook last name. Please try again';
                echo json_encode($data);
                return;
            }

            Mage::helper('inchoo_socialconnect/facebook')->connectByCreatingAccount($email,$firstName,$lastName,$userInfo->id,$token);

            Mage::getSingleton('core/session')->addSuccess('Your Facebook account is now connected to your new user accout at our store. Now you can login using our Facebook Connect button or using store account credentials you will receive to your email address.');
            $data['responseCode']='1';
            $data['msg']='Your Facebook account is now connected to your new user accout at our store. Now you can login using our Facebook Connect button or using store account credentials you will receive to your email address';
            echo json_encode($data);
            return;
//end here facebook code
  
    }
  
    else
    {
        $data['responseCode']='0';
        $data['msg']='Please Enter Appropriate Fields';
        echo json_encode($data);
return;      
        }
  
 ?>

2 comments


EmoticonEmoticon