How to Use OmniAuth Properly with Rails 2

坚强是说给别人听的谎言 提交于 2020-01-15 06:24:06

问题


I am trying to use an OmniAuth (0.2.6) strategy for my application. The architecture is Rails 2.3.10 and Rack is version is 1.1 (this version or higher is required for OmniAuth). A problem that occurs is Rails doesn't recognize my redirect to "/auth/provider". The actual error message is "No route matches "/auth/casport". Even though that error gets thrown, the code seems to execute past that point up to this error: "request.env['omniauth.auth']", which I'm pretty sure means it doesn't recognize the "omniauth.auth" key for the env hash. I have a feeling that OmniAuth isn't being loaded properly. I've tested the same strategy and code base with Mongrel and Webrick, and I get identical error messages.

Here is my OmniAuth initializer (config/initializers/omniauth.rb):

ActionController::Dispatcher.middleware.use OmniAuth::Builder do
  provider :casport, :setup => true
end

I definitely have routes in places for auth/casport/setup and auth/casport/callback.

I've also tried this approach: How do you implement OmniAuth on Rails 2.2?

config.middleware.use OmniAuth::Builder do
  provider :casport, :setup => true
end

by placing that middleware code in the config/environments/development.rb file.

Does anyone have any thoughts on what I could be doing incorrectly? Thanks!

Edit: I've also tried this approach Has anyone used omniauth with rails 2.3.8?

ActionController::Dispatcher.middleware.use OmniAuth::Strategies::Casport = {
  :setup => true
}

and that returns a read error: "NoMethodError: undefined method 'new' for #Hash...

Edit 2: I've upgraded to Rails 2.3.10 as OmniAuth isn't compatible with Rails 2.3.4.


回答1:


I created an issue on OmniAuth's Github page https://github.com/intridea/omniauth/issues/397, with the above details and this is the response from Michael Bleigh, the creator of OmniAuth:

"OmniAuth has never officially supported Rails 2.3.x -- I've heard that some people have had luck with it but it's not something that is guaranteed. Sorry!"

So it looks like the methods I posted above may work for some folks. To guarantee correct operation, Rails 3 is needed.



来源:https://stackoverflow.com/questions/6686780/how-to-use-omniauth-properly-with-rails-2

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