5
votes

We have registration and login via facebook using rails, devise, and omniauth. We're hosted on Heroku, running two web dynos.

sometimes 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.

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
    @user = User.find_for_facebook(env["omniauth.auth"].extra.raw_info, current_user)

    logger.info ">>>>>> OMNIAUTH FB BEGIN >>>>>>"
    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 => "Facebook"
    res = sign_in(:user, @user)

    logger.info "Sign In Result: #{res.inspect}"
    logger.info "CURRENT_USER: #{current_user.inspect}"
    logger.info "<<<<<< OMNIAUTH FB END <<<<<<"

    respond_to do |format|
      format.json
    end
  end
end

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.

We're on

  • Rails 3.2
  • Devise 2.1.2
  • Omniauth-facebook 1.4.0
  • Ruby 1.9.3 p194
1
got a very similar problem (in mine the current_user is always nil). Some people suggest to turn of forgery protection, take a look here: github.com/plataformatec/devise/wiki/OmniAuth:-Overview#google - chrmod
Please post your find_for_facebook definition from your User module. - xvidun
I have the same problem, after devise sign_in, the current_user is nil for a while. After few seconds the current_user is set? any advise on how to solve this issue - Oatmeal

1 Answers

-2
votes

I don't know whether this is really the solution to what you've described, but I wonder whether this:

@user = User.find_for_facebook(env["omniauth.auth"].extra.raw_info, current_user)

should read as this?

@user = User.find_for_facebook(request.env["omniauth.auth"].extra.raw_info, current_user)

that's how mine is anyways. NOt really sure that would explain why your app is arbitrarily letting the user sign in however.