title: How to login to Facebook with a login button?
body: <p>My app has a Facebook login button (<code>&lt;fb:login-button&gt;</code>) which logs the user into Facebook and authenticates their use of my app.  Here is my code (<code>login()</code> and <code>logout()</code> prepare the page layout for being logged in and logged out, respectively).  See <a href="https://stackoverflow.com/a/17539470/1397061">this answer</a> for the reasoning behind this code.</p>
  
  <pre><code>window.fbAsyncInit = function() {
     FB.init({
          appId: myAppID,
          channelUrl: window.location.protocol + '//' 
              + window.location.host + '/channel.html',
          status: false,
          cookie: true,
          xfbml: false,
          oauth: true
      });
  
      FB.getLoginStatus(function(response) {
          if (response.status === 'connected') {
              login(response);
          } else {
              FB.Event.subscribe('auth.login', function(response) {
                  login(response);
              });
              logout();
          }
      });
  }
  
  (function(d){
       var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
       if (d.getElementById(id)) return;
       js = d.createElement('script'); js.id = id; js.async = true;
       js.src = "//connect.facebook.net/en_US/all.js";
       ref.parentNode.insertBefore(js, ref);
  }(document));
  </code></pre>
  
  <p>The problem: new users get the Oauth dialog and can log into Facebook, but <code>auth.login</code> doesn't fire for them.  Neither do <code>auth.authResponseChange</code> and <code>auth.statusChange</code> (all described <a href="https://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/" rel="nofollow noreferrer">here</a>).  The next time the user presses the button, or if they were already logged into Facebook, <strong>the dialog pops up and immediately disappears</strong>.  (Strangely, the callbacks do occur as expected with my own Facebook account, perhaps because I've authenticated the app previously.)</p>
  
  <p>As a work-around, I've tried triggering <code>FB.login()</code> as a manual onclick event for the login-button (pardon the use of inline Javascript):</p>
  
  <pre><code>&lt;fb:login-button onclick="FB.login(function(r){if(r.authResponse)login(r)})"&gt;
  </code></pre>
  
  <p>but this doesn't trigger the login callback either (though it does log the user in).</p>
  
  <p><strong>What is the correct way to login to Facebook with a login button?</strong></p>