简体   繁体   中英

Devise + Google Auth + Mountable Engine Routing Error

I am building a rails app for authentication purpose. Here i used rails engine which contains whole authentication process for that i am using googleauth with devise. Problem: When user clicks sign_in _with_google it successfully redirects gmail login after successful login it will redirect to http://localhost:3000/users/auth/google_oauth2/callback?...... here is the problem it shows

Routing Error
uninitialized constant OmniauthCallbacksController

my_engine/routes.rb

devise_for :users, :controllers => { :omniauth_callbacks => "omniauth_callbacks" }, 
  :class_name => "MyEngine::User", module: :devise

omniauth_callbacks_controller.rb

require_dependency "my_engine/application_controller"

module MyEngine
  class OmniauthCallbacksController < Devise::OmniauthCallbacksController   
    def googleoauth2
      .....
    end
  end
end

login.html.erb

<%= link_to "Sign in with Google", my_engine.user_omniauth_authorize_path(:google_oauth2) %>

If any information needed please comment it i will give Thanks in Advance

development.log

Started GET "/" for 127.0.0.1 at 2014-10-13 18:25:20 +0530
  [1m[36mActiveRecord::SchemaMigration Load (21.0ms)[0m  [1mSELECT `schema_migrations`.* FROM `schema_migrations`[0m
Processing by MyEngine::DashboardController#index as HTML
Redirected to http://localhost:3000/test/home
Completed 302 Found in 62ms (ActiveRecord: 0.0ms)


Started GET "/" for 127.0.0.1 at 2014-10-13 18:25:21 +0530
Processing by MyEngine::DashboardController#index as HTML
Redirected to http://localhost:3000/test/home
Completed 302 Found in 1ms (ActiveRecord: 0.0ms)


Started GET "/test/home" for 127.0.0.1 at 2014-10-13 18:25:21 +0530
Processing by TestController#home as HTML
Completed 401 Unauthorized in 26ms


Started GET "/users/sign_in" for 127.0.0.1 at 2014-10-13 18:25:21 +0530
Processing by Devise::SessionsController#new as HTML
  Rendered /Users/user/Documents/NewApps/Rails4.0.10/fd/MyEngine/app/views/devise/sessions/new.html.erb within layouts/my_engine/application (32.4ms)
Completed 200 OK in 245ms (Views: 178.1ms | ActiveRecord: 46.9ms)


Started GET "/assets/my_engine/dashboard.css?body=1" for 127.0.0.1 at 2014-10-13 18:25:22 +0530


Started GET "/assets/my_engine/omniauth_callbacks.css?body=1" for 127.0.0.1 at 2014-10-13 18:25:22 +0530


Started GET "/assets/my_engine/application.css?body=1" for 127.0.0.1 at 2014-10-13 18:25:22 +0530


Started GET "/assets/my_engine/engine_layout.css?body=1" for 127.0.0.1 at 2014-10-13 18:25:22 +0530


Started GET "/assets/my_engine/application.js?body=1" for 127.0.0.1 at 2014-10-13 18:25:22 +0530


Started GET "/assets/my_engine/dashboard.js?body=1" for 127.0.0.1 at 2014-10-13 18:25:22 +0530


Started GET "/assets/my_engine/omniauth_callbacks.js?body=1" for 127.0.0.1 at 2014-10-13 18:25:22 +0530


Started GET "/assets/my_engine/logo.png" for 127.0.0.1 at 2014-10-13 18:25:22 +0530


Started GET "/assets/my_engine/btn_red_32.png" for 127.0.0.1 at 2014-10-13 18:25:22 +0530


Started GET "/users/auth/google_oauth2" for 127.0.0.1 at 2014-10-13 18:25:26 +0530


Started GET "/users/auth/google_oauth2/callback?state=4081132f9229erea9bb19b9174cce33fa7asdf5c3f708f96e1862020&code=4/Fd9y3m28CTrX_L1a9yeBMAsdcfer0V05rMv6UdfUmE23vfw8sdfyxI.4jR45xWflI4Tcp7tdiljKKbtbisTkgI" for 127.0.0.1 at 2014-10-13 18:25:27 +0530

ActionController::RoutingError (uninitialized constant OmniauthCallbacksController):
  activesupport (4.1.6) lib/active_support/inflector/methods.rb:238:in `const_get'
  activesupport (4.1.6) lib/active_support/inflector/methods.rb:238:in `block in constantize'
  activesupport (4.1.6) lib/active_support/inflector/methods.rb:236:in `each'
  activesupport (4.1.6) lib/active_support/inflector/methods.rb:236:in `inject'
  activesupport (4.1.6) lib/active_support/inflector/methods.rb:236:in `constantize'
  actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:78:in `controller_reference'
  actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:68:in `controller'
  actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:46:in `call'
  actionpack (4.1.6) lib/action_dispatch/routing/mapper.rb:45:in `call'
  actionpack (4.1.6) lib/action_dispatch/journey/router.rb:73:in `block in call'
  actionpack (4.1.6) lib/action_dispatch/journey/router.rb:59:in `each'
  actionpack (4.1.6) lib/action_dispatch/journey/router.rb:59:in `call'
  actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:678:in `call'
  railties (4.1.6) lib/rails/engine.rb:514:in `call'
  railties (4.1.6) lib/rails/railtie.rb:194:in `public_send'
  railties (4.1.6) lib/rails/railtie.rb:194:in `method_missing'
  actionpack (4.1.6) lib/action_dispatch/journey/router.rb:73:in `block in call'
  actionpack (4.1.6) lib/action_dispatch/journey/router.rb:59:in `each'
  actionpack (4.1.6) lib/action_dispatch/journey/router.rb:59:in `call'
  actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:678:in `call'
  omniauth (1.2.2) lib/omniauth/strategy.rb:404:in `call_app!'
  omniauth (1.2.2) lib/omniauth/strategy.rb:362:in `callback_phase'
  omniauth-oauth2 (1.2.0) lib/omniauth/strategies/oauth2.rb:77:in `callback_phase'
  omniauth (1.2.2) lib/omniauth/strategy.rb:227:in `callback_call'
  omniauth (1.2.2) lib/omniauth/strategy.rb:184:in `call!'
  omniauth (1.2.2) lib/omniauth/strategy.rb:164:in `call'
  warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.3) lib/warden/manager.rb:34:in `catch'
  warden (1.2.3) lib/warden/manager.rb:34:in `call'
  rack (1.5.2) lib/rack/etag.rb:23:in `call'
  rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
  rack (1.5.2) lib/rack/head.rb:11:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/flash.rb:254:in `call'
  rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/cookies.rb:560:in `call'
  activerecord (4.1.6) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
  activerecord (4.1.6) lib/active_record/migration.rb:380:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.1.6) lib/active_support/callbacks.rb:82:in `run_callbacks'
  actionpack (4.1.6) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.1.6) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.1.6) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.1.6) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.1.6) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.1.6) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/static.rb:64:in `call'
  rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
  railties (4.1.6) lib/rails/engine.rb:514:in `call'
  railties (4.1.6) lib/rails/application.rb:144:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
  /Users/user/.rvm/rubies/ruby-1.9.3-p547/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
  /Users/user/.rvm/rubies/ruby-1.9.3-p547/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
  /Users/user/.rvm/rubies/ruby-1.9.3-p547/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'


  Rendered /Users/user/.rvm/gems/ruby-1.9.3-p547@rails4.1.6/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.2ms)
  Rendered /Users/user/.rvm/gems/ruby-1.9.3-p547@rails4.1.6/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/routes/_route.html.erb (0.8ms)
  Rendered /Users/user/.rvm/gems/ruby-1.9.3-p547@rails4.1.6/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/routes/_route.html.erb (2.2ms)
  Rendered /Users/user/.rvm/gems/ruby-1.9.3-p547@rails4.1.6/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/routes/_table.html.erb (41.3ms)
  Rendered /Users/user/.rvm/gems/ruby-1.9.3-p547@rails4.1.6/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (93.8ms)

omniauth_callbacks_controller.rb

 def googleoauth2  
  @auth = env[OMNI_AUTH]
  @auth_user = AuthUsers.find_by_email(@auth.info.email)
  unless @auth_user
    flash[:error] = "Sorry, you are not authorized."
    redirect_to "/users/sign_in"
  end
  @signed_up_user = User.find_by_email(@auth.info.email)
  email = @auth.info.email
  unless @signed_up_user
    @user = User.find_for_google_oauth2(request.env[OMNI_AUTH], current_user)
  end
  if @auth_user && @signed_up_user
    @user = User.find_for_google_oauth2(request.env[OMNI_AUTH], current_user)
    sign_in_and_redirect @user, :event => :authentication
  end
end

Directory Structure::

MyEngine ->app -> controllers -> my_engine -> omniauth_callbacks_controller.rb

changes in my_engine/routes.rb

devise_for :users, :controllers => { :omniauth_callbacks => "omniauth_callbacks" }, :class_name => "MyEngine::User", module: :devise

to

devise_for :users, :controllers => { :omniauth_callbacks => "my_engine/omniauth_callbacks" }, :class_name => "MyEngine::User", module: :devise

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM