Question

Couldn't find similar enough question so I'll ask.

I'm trying to use facebook log-in on my page.

I did everything according the the developer's guides. However, I'm not sure when I can start calling FB.API calls. when is the user authenticated? how can I know when this operation was done?

<script>
    window.fbAsyncInit = function () {
        FB.init({
            appId: 'someID', // App ID
            channelUrl: '//www.domain.com/channel.html', // Channel File
            status: true, // check login status
            cookie: true, // enable cookies to allow the server to access the session
            xfbml: true  // parse XFBML
        });

    };

    // Load the SDK Asynchronously
    (function (d) {
        var js, id = 'facebook-jssdk'; if (d.getElementById(id)) { return; }
        js = d.createElement('script'); js.id = id; js.async = true;
        js.src = "//connect.facebook.net/en_US/all.js";
        d.getElementsByTagName('head')[0].appendChild(js);
    } (document));
</script>

now I want to call this

    FB.api('/me', function (response) {
        facebookUser = response; 
        alert('Your name is ' + response.name);
    });

but not sure when is the write time. I get undefined wherever I put it :/

Was it helpful?

Solution

You will want to run the FB.getLoginStatus() (https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/) before you send the user to log in using FB.login() or if the user is logged in, to call the FB.api() method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top