Search code examples
javascriptfacebook-php-sdkfacebook-social-pluginsfacebook-login

FB Login button in HTML 5 with JS redirect


In the older version of the FB Login button, this worked well for me

<fb:login-button scope='email,publish_stream'>Login</fb:login-button>
<div id='fb-root'></div>

 <script type='text/javascript' src='http://connect.facebook.net/en_US/all.js'></script>     
 <script>window.fbAsyncInit = function() {FB.init({
      appId: '<?php echo $facebook->getAppID(); ?>',
      cookie: true,
      xfbml: true,
      oauth: true
   });
   FB.Event.subscribe('auth.login', function(response) {
      window.location = '/fb_redirect.php';
   });
  FB.Event.subscribe('auth.logout', function(response) {
      window.location.reload();
  });
   };
  (function() {
     var e = document.createElement('script'); 
      e.async = true;e.src =   document.location.protocol +'//connect.facebook.net/en_US/all.js';document.getElementById('fb-root').appendChild(e);}());
  </script>

But, a friend of mine recently suggested switching to the new HTML 5 Login button as follows:

  <div id='fb-root'></div>
        <script>
            (function(d, s, id) {
                var js, fjs = d.getElementsByTagName(s)[0];
                if (d.getElementById(id)) return;
                js = d.createElement(s); js.id = id;
                js.src = '//connect.facebook.net/en_US/all.js#xfbml=1&appId=APPID';
                fjs.parentNode.insertBefore(js, fjs);
            }(document, 'script', 'facebook-jssdk'));
        </script>

This works well too. Except that I can't find a way for JS to trigger a redirect to /fb_redirect.php. In the older example,

FB.Event.subscribe('auth.login', function(response) {
      window.location = '/fb_redirect.php';
   });

worked just fine. But, I need for the user to be redirected to /fb_redirect.php so that PHP can start a session and insert some of the user's FB info into the DB.


Solution

  • This turned out to be a signature careless error of mine. The answer was in the first part of this question.

    It simply was to add

    FB.Event.subscribe('auth.login', function(response) {
      window.location = '/fb_redirect.php';
    

    });