1

I'm developing a facebook JS+PHP user login on a Cordova app with netbeans, testing on chrome with NB connector.

This is the client side code:

 $(document).ready(function () {
            $.ajaxSetup({cache: true});
            $.getScript('//connect.facebook.net/en_US/sdk.js', function () {
                FB.init({
                    appId: 'xxxxxxxxxxxxxxxxxx',
                    version: 'v2.5', // or v2.0, v2.1, v2.2, v2.3
                    status     : true,
                    cookie: true
                });
                $('#loginbutton,#feedbutton').removeAttr('disabled');
                FB.getLoginStatus(function (response) {
                    if (response.status === 'connected') {
                        console.log("logged in");
                    } else {
                        console.log("this user is not logged in yet");
                    }
                });
            });
        });
        function doFacebookLogin() {
            FB.getLoginStatus(function (response) {
                if (response.status === 'connected') {
                    console.log("token " + response.authResponse.accessToken);

                    $.ajax({
                        url: "http://mywebsite/fb_login.php",
                        crossDomain: true,
                        jsonpCallback: 'callb',
                        dataType: 'jsonp',
                        success: function (response) {
                            console.log('callback success' + response);
                            console.log(response);

                        },
                        error: function (xhr, status, error) {
                            console.log(status + '; ' + error);
                            alert(status + ' | ' + error + ' | ' + xhr);
                        }
                    });
                } else {
                    FB.login(function (response) {
                        if (response.status === 'connected') {
                            FB.getLoginStatus(function (response) {
                                if (response.status === 'connected') {
                                    console.log("token " + response.authResponse.accessToken);
                                    $.ajax({
                                        url: "http://mywebsite/fb_login.php",
                                        crossDomain: true,
                                        jsonpCallback: 'callb',
                                        dataType: 'jsonp',
                                        success: function (response) {
                                            console.log('callback success' + response);
                                        },
                                        error: function (xhr, status, error) {
                                            console.log(status + '; ' + error);
                                            alert(status + ' | ' + error + ' | ' + xhr);
                                        }
                                    });
                                }
                            });
                        } else if (response.status === 'not_authorized') {
                            console.log("this user does not have approved this app yet!");
                        } else {
                            console.log("this user is not even logged to facebook!");
                        }
                    });
                }
            });

        }
        function callb(json) {
            console.log("Callback was called!")
        }

There is a button for the user to manually login to facebook if he is not logged or is the first time.

this is the server side code:

    error_reporting(-1);
ini_set('display_errors', 'On');
# /js-login.php
require_once __DIR__ . '/facebook-sdk-v5/autoload.php';

$fb = new Facebook\Facebook([
    'app_id' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxx',
    'app_secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    'default_graph_version' => 'v2.5',
        ]);

$helper = $fb->getJavaScriptHelper();
$response = "";
//$response.= print_r($_COOKIE,true);
try {
    $accessToken = $helper->getAccessToken();
} catch (Facebook\Exceptions\FacebookResponseException $e) {
    // When Graph returns an error
    $response .= 'Graph returned an error: ' . $e->getMessage();
    outputJsonP($response);
    exit;
} catch (Facebook\Exceptions\FacebookSDKException $e) {
    // When validation fails or other local issues
    $response .= 'Facebook SDK returned an error: ' . $e->getMessage();
    outputJsonP($response);
    exit;
}

if (!isset($accessToken)) {
    $response .= 'error: No cookie set or no OAuth data could be obtained from cookie.';
    outputJsonP($response);
    exit;
}

// Logged in
//var_dump($accessToken->getValue());

$_SESSION['fb_access_token'] = (string) $accessToken;
outputJsonP($response);

function outputJsonP($response) {
    $myArray = array(
        "response" => $response,
    );
    $myJSONString = json_encode($myArray);
    echo 'callb(' . $myJSONString . ');';
}

I always obtain

error: No cookie set or no OAuth data could be obtained from cookie.

So i guess or the cookie is not coming to server side or the oAuth process is not going well. I looked at the result of

print_r($_COOKIE,true)

but i cannot find the cookie. it's also true i don't really know well what to search for...

Probably the jsonP ajax request does not allow the cookie to go to the server? If so how can i do this?

Can someone help me to better identify my problem or solve it? thanks!

Edit: maybe can be related to this?Cannot set cookies in Javascript

Community
  • 1
  • 1
Romeo
  • 1,135
  • 13
  • 26

0 Answers0