A before_filter for Omniauth Facebook?

戏子无情 提交于 2019-12-24 06:45:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!