Welcome Guest, Not a member yet? Register   Sign In
Facebook login
#1

Hello guys, sorry for the question, which I have already tried immensely on google but I could not make it with any tutorial present. I would like to create a facebook login, but it seems that every tutorial is not fully functional.

Can you give me a guide or a small example of this? I would simply like a login with an obvious system of permissions (if logged in or not). I've tried a lot of guides, but it seems to always miss something or always make a mistake.
Reply
#2

I used this library:
https://github.com/facebook/php-graph-sdk/contributors

and put it to application/third_party folder

usage:
1. I created Facebook.php library in appication/library:
PHP Code:
class Facebook {

    public function 
__construct($config  array()) {

        require_once 
APPPATH.'third_party/facebook/vendor/autoload.php';
        
$this->fb = new Facebook\Facebook($config);
        
$this->permissions = ['public_profile','email'];
        
$this->fb_helper $this->fb->getRedirectLoginHelper();

    }
    
    public function 
loginURL($url) {
        return 
$this->fb_helper->getLoginUrl($url$this->permissions);
    }
    
    public function 
authenticate($code null) {
        return 
$this->client->authenticate($code);
    }
    
    public function 
getAccessToken() {
        try{
            
$token $this->fb_helper->getAccessToken();
            return array(
'token' => $token'message' => '');

        }catch(
Facebook\Exceptions\FacebookResponseException $e)
        {
            
// When Graph returns an error
            
return array('token' => false,
                
'message' => 'There was an error while trying to login using Facebook: ' $e->getMessage()
                );
        }catch(
Facebook\Exceptions\FacebookSDKException $e)
        {
 
           // When validation fails or other local issues
            
return array('token' => false,
                
'message' => 'Facebook SDK returned an error: ' $e->getMessage()
                );
        }
    }
    
    public function 
setAccessToken($token) {
        if (!empty(
$token)) {
            
$this->fb->setDefaultAccessToken($token);
        }
    }
    
    public function 
revokeToken() {
        return 
$this->client->revokeToken();
    }
    
    public function 
getUserInfo() {
        try{
            
$response $this->fb->get('/me?fields=id,first_name,last_name,name,email,verified');
            return array(
'user_info' => $response->getGraphUser(), 'message' => '');

        }catch(
Facebook\Exceptions\FacebookResponseException $e)
        {
 
         // When Graph returns an error
            
return array('user_info' => false,
                
'message' => 'Could not retrieve user data: ' $e->getMessage()
                );
        }
        catch(
Facebook\Exceptions\FacebookSDKException $e)
        {
 
         // When validation fails or other local issues
            
return array('user_info' => false,
                
'message' => 'Facebook SDK returned an error: ' $e->getMessage()
                );
        }
    }
    

 
2. In login controller:

PHP Code:
   public function index(){
 
       //...
 
       $this->load->library('facebook'$this->config->item('fb_config'));
$callbackurltoyourwebsite base_url() . 'login/facebookLogin';
 
       $data['facebook_login_link'] = $this->facebook->loginURL($callbackurltoyourwebsite);
 
       //facebook_login_link will be used in view as a facebook login button 
 
   
3. in login controller:
PHP Code:
   public function facebookLogin() 
 
   {
 
       $this->load->library('facebook'$this->config->item('fb_config'));
 
       $response $this->facebook->getAccessToken();
 
       if($response['token'])
 
       {
 
           $token $response['token'];
 
           $this->facebook->setAccessToken($token);
 
       }else{
 
           error_log($response['message']);
 
       }
 
       if(!empty($token))
 
       {
 
           $response $this->facebook->getUserInfo();
 
           if($response['user_info']){
 
               $fbuser $response['user_info'];
 
               $member_data = array();
 
               if ($fbuser['id'] && $fbuser['verified']) 
 
               {
 
                   $login_data['email'] = $fbuser['email'];
 
                   $member_data $this->member_model->getmember($login_data);
 
                   if (empty($member_data)) //if there is no member with the same email address
 
                   {
 
                       //insert new member 
 
                       //then:
 
                       $this->_set_session_data($member_data);
 
                   }
 
                   else{
 
                       $this->_set_session_data($member_data);
 
                           redirect();
 
                   }
 
               }else{

 
               }
 
           }else{
 
           }
 
       }
 
   

your facebook configs in applicaton/config/config.php:

PHP Code:
$config['fb_config'] = array(
    
'app_id'  => '',
    
'app_secret' => '',
    
'default_graph_version' => 'v2.5'); 
Reply
#3

Thanks for your help, can you tell me where to upload the facebook sdk? And how to call the folder?
Reply
#4

(01-16-2018, 12:16 PM)Marcolino92 Wrote: Thanks for your help, can you tell me where to upload the facebook sdk? And how to call the folder?

Everything is in my answer read carefully
Reply
#5

Hello,

You can use my library : https://github.com/bvrignaud/ci-fb-ion-auth
Reply
#6

Hi guys, I tried with a library but this error appears. It does not find the class. I inserted the Facebook "Facebook" SDK folder inside the Library. Where am I wrong?

An uncaught Exception was encountered

Type: Error

Message: Class 'Facebook\Facebook' not found

Filename: /home/mhd-01/www.augustani.it/htdocs/application/libraries/Facebook.php
Line Number: 65
Reply




Theme © iAndrew 2016 - Forum software by © MyBB