5

Identification by Facebook

The identification and registration forms can directly obtain an email address from a Facebook account. Install the Facebook SDK for PHP then configure the parameters $facebookid and $facebooksecret in config.inc and set the option $with_facebook of the blocks login and register to true.

NOTE: The current code interfaces the version 4 or 5 of the SDK which is compatible with PHP 5.4 and PHP 7. If you are stuck with PHP 5.3, you can try to retrieve an earlier version of the code for the functions login and register and install the SDK 3 in a folder called facebook at the root of the website.

Facebook SDK

Install Composer.

Go to the root of the website and type the following command:

$ composer require facebook/graph-sdk

All the code is in the directory vendor.

NOTE: Add the files composer.json and composer.lock to Git. Add the directory vendor in .gitignore.

From your Facebook account, go to the part facebook for developers. Add an application. Enter the domain name and the email address fo the webmaster, e.g. sitename.net and webmaster@sitename.net. Create an identifier for the application.

In Parameters, copy the Id and the secret key of the application in the parameters $facebookid and $facebooksecret in config.inc.

Add the product Facebook Login. In the parameters of the product, in the field Valid OAuth redirection URI, specify the addresses of the identification and registration pages of the site, in all the supported languages, in HTTP and in HTTPS if necessary.

IMPORTANT: Examin the application and make it public.

login.php and register.php

Edit the files login.php and register.php. Set the parameter $with_facebook to true to activate the interface with Facebook.

The code obtains the user's email address from Facebook then validates it as if the user had typed it. In the idenfication form, it skips checking the password. In the registration form, if the parameter $with_info is true, it will also extract the user's last name and first name from the Facebook profile.

Code

The code in login.php shows how the dialog with Facebook is managed. The code in register.php is identical except for the options $with_info and $with_webiste which ask for the user's firstname and lastname as well as the address of her website.

    if ($with_facebook) {
        require_once 'vendor/autoload.php';

        global $facebookid, $facebooksecret;

        $facebook=new \Facebook\Facebook(array('app_id' => $facebookid, 'app_secret' => $facebooksecret));
    }

If $with_facebook is true, loads the code of the SDK and creates an instance of Facebook with the configuration parameters $facebookid and $facebooksecret.

    switch($action) {
        case 'init':
            if ($with_facebook) {
                $helper = $facebook->getRedirectLoginHelper();
                try {
                    $accessToken = $helper->getAccessToken();

                    if ($accessToken) {
                        $fields=array('email');

                        $r = $facebook->get('/me?fields=' . implode(',', $fields), $accessToken);
                        $user = $r->getGraphUser();

                        $login=$user['email'];

                        $action='facebook';
                    }
                }
                catch(\Facebook\Exceptions\FacebookResponseException $e) {
                }
                catch(\Facebook\Exceptions\FacebookSDKException $e) {
                }
            }
            break;

If $with_facebook is true, extracts the access token to Facebook. If the token has a value, the user is connected and the code can recover the field email. If everything goes well, without exceptions, sets $action to facebook, a variation of the action register, in order to analyze the form as normal but without verifying a password.

    $connectbar=false;
    if ($with_facebook) {
        global $base_url;

        $url=$base_url . url('user', $lang);
        $scope = array('email');
        $helper = $facebook->getRedirectLoginHelper();
        $facebook_login_url=$helper->getLoginUrl($url, $scope);
        $connectbar=view('connect', $lang, compact('facebook_login_url'));
    }

The parameter $connectbar of the view login contains the HTML code of the button calling Facebook. If $with_facebook is true, asks the Facebook object the URL which must be associated to the button and passes it to the view connect.

Comments

To add a comment, click here.