Search code examples
ruby-on-railsrubyruby-on-rails-3omniauthcomfortable-mexican-sofa

Rails3 CMS comfortable-mexican-sofa get mismatching routing with omniauth


I integrated comfortable-mexican-sofa CMS with my Rails 3.0.9 app with omniauth 0.2.6 in place.

Everything work fine blog side. I can log in cms-admin, working with the admin console, make a post and log off ( cms-admin has it's own authentication system, different by my app ... ), than I can browse the new post.

The problem is that the rest of my app, authenticate users with omniauth, and when I log off/on I get the following error:

Started GET "/auth/github" for 127.0.0.1 at 2011-07-25 21:04:46 +0200
  Processing by CmsContentController#render_html as HTML
  Parameters: {"cms_path"=>"auth/github"}
  SQL (0.3ms)  SELECT COUNT(*) FROM "cms_sites"
  Cms::Site Load (0.3ms)  SELECT "cms_sites".* FROM "cms_sites" LIMIT 1
Completed 500 Internal Server Error in 156ms

NoMethodError (undefined method `gsub!' for nil:NilClass):

My Gemfile.lock is here: http://pastie.org/2270005

Any help will be greatly appreciated. Luca G. Soave


Solution

  • It was a routing problem.

    Omniauth works by detecting a 404 error page:

    if it matches the route /auth/:provider, then it catches the request sends it to the provider

    ... but comfortable-mexican-sofa cms (like refinery and others), has a catch all route, which is why the request never returned a 404 error, which omniauth could detect.

    In comfortable-mexican-sofa case, it goes through "cms_path"=>"auth/github" getting

    NoMethodError in CmsContentController#render_html
    undefined method `gsub!' for nil:NilClass
    

    instead of the correct omniauth path which should go like following:

    Started GET "/" for 127.0.0.1 at 2011-07-25 22:27:26 +0200
      Processing by HomeController#index as HTML
    Rendered home/index.html.haml within layouts/application (29.5ms)
    Completed 200 OK in 44ms (Views: 42.8ms | ActiveRecord: 0.0ms)
    
    
    Started GET "/auth/github" for 127.0.0.1 at 2011-07-25 22:27:35 +0200
    MONGODB gitwatch_dev['users'].find({:provider=>"github", :uid=>1573})
    
    
    Started GET "/auth/github/callback?code=4334bab983hd5fec19dd" for 127.0.0.1 at 2011-07-25 22:27:36 +0200
      Processing by SessionsController#create as HTML
      Parameters: {"code"=>"4334bab983hd5fec19dd", "provider"=>"github"}
    Redirected to http://localhost:3001/
    Completed 302 Found in 255ms
    MONGODB gitwatch_dev['users'].find({:_id=>BSON::ObjectId('4e23114b1d41c80f180005b2')})
    
    
    Started GET "/" for 127.0.0.1 at 2011-07-25 22:27:39 +0200
      Processing by HomeController#index as HTML
    Rendered home/index.html.haml within layouts/application (415.6ms)
    Completed 200 OK in 890ms (Views: 428.6ms | ActiveRecord: 0.0ms)
    

    The solution in my case is like the following :

    in app/controllers/errors_controller.rb

    class ErrorsController < ApplicationController
      def error
        render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
      end
    end
    

    in config/routes.rb add at the bottom

    match '/auth/:provider' => 'errors#error'

    ... after Omniouth last route

    `match "/auth/:provider/callback" => "sessions#create"`
    

    thanks goes to Federico Gonzalez and to Oleg Khabarov

    I hope this will help someone else ;-)

    bye Luca G. Soave