title: Codeigniter, Facebook javascript SDK, PHP SDK Redirect after facebook login doesn't getUser() until refresh
body: <p>The process is as follows.</p>
  
  <pre><code>1. User clicks Facebook Login button
  2. User signs in and grants perms
  3. Facebook redirects uses FB.Event.subscribe auth.login to redirect my login controller
  4. My login controller loads the fbconnect module and chains the method Connect()
  5. Connect() Checks if there is a facebook user using getUser() and if true checks if they are an existing user to log them in or returns false if no facebook user was found.
  </code></pre>
  
  <p>Everything works except the only problem is after being redirected to the login page Connect returns False when it should in fact be true sine the fbuser is logged in and has granted perms. If I refresh however everything works, the user is logged in if exists or if new is sent to the signup page. Below is my code..</p>
  
  <pre><code>      &lt;div id="fb-root"&gt;&lt;/div&gt;
      &lt;script&gt;
        //initializing API
        window.fbAsyncInit = function() {
          FB.init({appId: '(My app id)', status: true, cookie: true,
                   xfbml: true, // channel.html file
                   oauth  : true
                   });
                   FB.Event.subscribe('auth.login', function(response) {
                   window.location='http://www.(Mysite).com/login';
                   });
                   FB.Event.subscribe('auth.logout', function(response) {
                   window.location='http://www.(Mysite).com/logout';
                   });
                   };
        (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);
        }());
      &lt;/script&gt;
  </code></pre>
  
  <p>Fbconnect.php</p>
  
  <pre><code>class Fbconnect extends MX_Controller {
  public $fb_user = null;
  public $fb_user_id = null;
  
  // Initilize Fabebook
  public function __construct() 
  {   
      parent::__construct();
      $this-&gt;config-&gt;load("facebook");
      require 'facebook.php';
  
      $this-&gt;facebook = new Facebook(array(
        'appId' =&gt; $this-&gt;config-&gt;item('appId'),
        'secret' =&gt; $this-&gt;config-&gt;item('Secret')
      ));
  
  
      // Get user from FB
      $this-&gt;fb_user_id = $this-&gt;facebook-&gt;getUser();
  
      if ($this-&gt;fb_user_id):
        try {
          // Proceed knowing you have a logged in user who's authenticated.
          $this-&gt;fb_user = (object)$this-&gt;facebook-&gt;api('/me');
        } catch (FacebookApiException $e) {
          error_log($e);
          $this-&gt;fb_user = null;
        }
      endif;
  
  }
  
  
  public function connect(){
  
      // Check for FB object
      if($this-&gt;fb_user):
          // FB Object found
              // Check if user exists
              $this-&gt;load-&gt;model('facebook/m_facebook');
              $user = $this-&gt;m_facebook-&gt;getUser($this-&gt;fb_user-&gt;id);
  
              // If we find a user and fb connect is true
              if($user):
                  // user found log them in
                  if($this-&gt;ion_auth-&gt;login($user-&gt;username, $user-&gt;password, true, true)):
                     redirect('', 'refresh');
                  endif;
              else:
              // There is a fb object &amp; the user is not logged in &amp; doesnt exist in db yet must be new 
                  redirect('signup/facebook', 'refresh');
              endif;
      else: 
          // No FB user or user not logged in                                  
          return false;
      endif;    
  }
  </code></pre>
  
  <p>The auth/login code (just the part that involves facebook)</p>
  
  <pre><code> $this-&gt;load-&gt;module('facebook/fbconnect')-&gt;connect(); 
  </code></pre>