Search code examples
ruby-on-railsrubyrequestomniauthomniauth-facebook

request.env["omniauth.auth"] returns nil if not called as argument


I'm trying to do a simple omniauth call with facebook. Everything works fine if I capture the request hash like this:

def facebook
    user = User.from_facebook(request.env["omniauth.auth"])
    if user.persisted?
      ...
    else
      ...
    end
  end

But if I try to save the request hash in a variable, I get request = nil. e.g.:

def facebook
  omni_request = request.env["omniauth.auth"]
  user = User.from_facebook(omni_request)
  if user.persisted?
    ...
  else
    ...
  end
end

The above example blows up because request is nil, and I can't call env on something that is nil.

Does anyone have any idea why request would be nil when called outside of the from_facebook class method?


Solution

  • the third line should be : user = User.from_facebook(omni_request) ?