问题
For devise there is a very easy to use Before_filter to use in the controllers.
For some reason I can't get this to work for the Omniauth_facebook Gem. I followed the Railscast on Facebook Omniauth and also
before_filter :authenticate
def authenticate
redirect_to :login unless User.find_by_provider_and_uid(auth["provider"], auth["uid"])
end
end
but I get an error:
NameError in PostsController#new
undefined local variable or method `auth' for #<PostsController:0x007f9fbfa7ee58>
Any thoughts?
回答1:
It can't find the variable named auth. So you need to check that auth variable initialized somewhere or not. As per my view, OmniAuth Facebook gem is storing the authenticated data in request env. Please refer this: https://github.com/mkdynamic/omniauth-facebook#auth-hash.
before_filter will execute before serving the request. so may be that causes the issues.
Hope that helps!!!
回答2:
I think the auth variable you're after is request.env["omniauth.auth"]
来源:https://stackoverflow.com/questions/16677247/a-before-filter-for-omniauth-facebook