title: Facebook Login Javascript SDK autologin if a cookie is set
body: <p>I am Trying to set up my website so that when you go onto the login page it checks if a cookie is set and if it is then the login should be automatic - i.e. when this page loads it redirects to "Facebook.ashx" which creates a cookie to remember access token.</p>
  
  <p>However if the cookie is not set then the user has to click on the Login with Facebook button to continue login which then continues to redirect to "Facebook.ashx" as before.</p>
  
  <p>This is my code - at the moment it always logs in once the user has accepted the app and continues to "Facebook.ashx"</p>
  
  <pre><code>&lt;div id="fb-root"&gt;&lt;/div&gt;  
  &lt;script type="text/javascript"&gt;
      window.fbAsyncInit = function () {
          FB.init({
              appId: 'My_App_ID', // App ID
              status: true, // check login status
              cookie: true, // enable cookies to allow the server to access the session
              xfbml: true  // parse XFBML
          });
  
          // Additional initialization code here
  
          FB.Event.subscribe('auth.authResponseChange', function (response) {
              if (response.status === 'connected') {
                  // the user is logged in and has authenticated your
                  // app, and response.authResponse supplies
                  // the user's ID, a valid access token, a signed
                  // request, and the time the access token 
                  // and signed request each expire
                  var uid = response.authResponse.userID;
                  var accessToken = response.authResponse.accessToken;
  
                  // TODO: Handle the access token
                  // Do a post to the server to finish the logon
                  // This is a form post since we don't want to use AJAX
                  var form = document.createElement("form");
                  var redirect = "FacebookLogin.ashx?redirect=" + document.getElementById('redirect').innerHTML;
                  form.setAttribute("method", 'post');
                  form.setAttribute("action", redirect);
  
                  var field = document.createElement("input");
                  field.setAttribute("type", "hidden");
                  field.setAttribute("name", 'accessToken');
                  field.setAttribute("value", accessToken);
                  form.appendChild(field);
  
                  document.body.appendChild(form);
                  form.submit();
  
              } else if (response.status === 'not_authorized') {
                  // the user is logged in to Facebook, 
                  // but has not authenticated your app
              } else {
                  // the user isn't logged in to Facebook.
              }
          });
      };
  
    // Load the SDK Asynchronously
    (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));
  &lt;/script&gt;
  
  &lt;div class="fb-login-button" scope="email, publish_stream"&gt;Login with Facebook&lt;/div&gt;
  </code></pre>
  
  <p>Thanks For any help in advance</p>