title: devise + omniauth facebook : current_user nil after sign_in
body: <p>We have registration and login via facebook using rails, devise, and omniauth. We're hosted on Heroku, running two web dynos.</p>
  
  <p><em>sometimes</em> login with facebook is failing.  The actual handshake between our app and facebook is fine. In fact, in the code below @user is an actual User model instance, the omniauth data Hash contains all of the data from FB, sign_in seems successful AND the current_user is set.</p>
  
  <pre><code>class Users::OmniauthCallbacksController &lt; Devise::OmniauthCallbacksController
    def facebook
      @user = User.find_for_facebook(env["omniauth.auth"].extra.raw_info, current_user)
  
      logger.info "&gt;&gt;&gt;&gt;&gt;&gt; OMNIAUTH FB BEGIN &gt;&gt;&gt;&gt;&gt;&gt;"
      logger.info env["omniauth.auth"].inspect
      logger.info "User is: #{@user.inspect}"
  
      session["devise.facebook_data"] = request.env["omniauth.auth"].except("extra")
  
      flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind =&gt; "Facebook"
      res = sign_in(:user, @user)
  
      logger.info "Sign In Result: #{res.inspect}"
      logger.info "CURRENT_USER: #{current_user.inspect}"
      logger.info "&lt;&lt;&lt;&lt;&lt;&lt; OMNIAUTH FB END &lt;&lt;&lt;&lt;&lt;&lt;"
  
      respond_to do |format|
        format.json
      end
    end
  end
  </code></pre>
  
  <p>The client-side does a redirect (different actions based on login context) to another URL on our site upon successful login. At that URL, we check for current_user... but current_user is returning nil.  If the login process is repeated a few times, eventually it works.</p>
  
  <p>We're on </p>
  
  <ul>
  <li>Rails 3.2</li>
  <li>Devise 2.1.2</li>
  <li>Omniauth-facebook 1.4.0</li>
  <li>Ruby 1.9.3 p194</li>
  </ul>