1
votes

I'm creating a Login Function using Facebook's SDK. I'm re-using code from a previous project that had a Login button which redirected to a Login Box on the Facebook Domain (i.e. the Login box was not a popup, but redirected the user).

In the previous project when the user would come back to the site after accepting the app, there was a PHP script which created a $_Session:

$user = $facebook->getUser();
if (isset($user)){
    $_SESSION['LoggedIn'] = $user;
}

I could then use the 'LoggedIn' session to check if the user was logged in or not, and modify the page based on that (e.g. replace content on the page).

Here's my question - I am now using the JS code that Facebook provides for a popup Login box. I'm guessing after the user Accepted the app from the Login Popup I need to start the session from within JavaScript? The problem is I can't figure out how....

 $(".facebookButton").click(function(){          
     FB.login(function(response) {
        if (response.authResponse) {
           //User accepted the app -I need to start the SESSION here?
         } else {
           //User hasn't accepted the app.
         }
      });
  });

Basically what I'm trying to achieve is for the site to know whether the user is logged in or not, even after they've refreshed the page. Thanks for the help!

1

1 Answers

0
votes

When the user logs in using the JavaScript SDK, a cookie is immediately dropped on your site with their auth details. The dropped cookie can also be ready by the PHP SDK so all you need to really do is refresh the page for the PHP SDK to detect the user:

$(".facebookButton").click(function(){          
   FB.login(function(response) {
      if (response.authResponse) {
         // reload page
         location.reload();
       } else {
         // User hasn't accepted the app.
       }
    });
});