Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cascade of Issues with Omniauth(?) #18

Closed
PeteMichaud opened this issue Aug 24, 2014 · 4 comments
Closed

Cascade of Issues with Omniauth(?) #18

PeteMichaud opened this issue Aug 24, 2014 · 4 comments

Comments

@PeteMichaud
Copy link

I have a series of issues. I have a project that mounts all the routes inside the api, eg:

new_api_v1_user_session GET /api/v1/auth/sign_in(.:format) devise_token_auth/sessions#new {:format=>:json}

When I try to authenticate against google_oauth2, I hit this route: http://localhost:5000/api/v1/auth/google_oauth2?auth_origin_url=http://localhost:5000/#/

and it throws this:

NoMethodError
undefined method `valid_encoding?' for :json:Symbol

actionpack (4.1.0) lib/action_dispatch/routing/redirection.rb:24:in block in call' actionpack (4.1.0) lib/action_dispatch/routing/redirection.rb:22:ineach'
actionpack (4.1.0) lib/action_dispatch/routing/redirection.rb:22:in call' actionpack (4.1.0) lib/action_dispatch/routing/mapper.rb:45:incall'
actionpack (4.1.0) lib/action_dispatch/journey/router.rb:71:in block in call' actionpack (4.1.0) lib/action_dispatch/journey/router.rb:59:ineach'
actionpack (4.1.0) lib/action_dispatch/journey/router.rb:59:in call' actionpack (4.1.0) lib/action_dispatch/routing/route_set.rb:676:incall'
omniauth (1.2.2) lib/omniauth/strategy.rb:186:in call!' omniauth (1.2.2) lib/omniauth/strategy.rb:164:incall'
omniauth (1.2.2) lib/omniauth/strategy.rb:186:in call!' omniauth (1.2.2) lib/omniauth/strategy.rb:164:incall'
omniauth (1.2.2) lib/omniauth/strategy.rb:186:in call!' omniauth (1.2.2) lib/omniauth/strategy.rb:164:incall'
omniauth (1.2.2) lib/omniauth/builder.rb:59:in call' rack-cors (0.2.9) lib/rack/cors.rb:54:incall'
warden (1.2.3) lib/warden/manager.rb:35:in block in call' warden (1.2.3) lib/warden/manager.rb:34:incatch'
warden (1.2.3) lib/warden/manager.rb:34:in call' rack (1.5.2) lib/rack/etag.rb:23:incall'
rack (1.5.2) lib/rack/conditionalget.rb:25:in call' rack (1.5.2) lib/rack/head.rb:11:incall'
remotipart (1.2.1) lib/remotipart/middleware.rb:27:in call' actionpack (4.1.0) lib/action_dispatch/middleware/params_parser.rb:27:incall'
actionpack (4.1.0) lib/action_dispatch/middleware/flash.rb:254:in call' rack (1.5.2) lib/rack/session/abstract/id.rb:225:incontext'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in call' actionpack (4.1.0) lib/action_dispatch/middleware/cookies.rb:560:incall'
activerecord (4.1.0) lib/active_record/query_cache.rb:36:in call' activerecord (4.1.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:incall'
activerecord (4.1.0) lib/active_record/migration.rb:380:in call' actionpack (4.1.0) lib/action_dispatch/middleware/callbacks.rb:29:inblock in call'
activesupport (4.1.0) lib/active_support/callbacks.rb:82:in run_callbacks' actionpack (4.1.0) lib/action_dispatch/middleware/callbacks.rb:27:incall'
actionpack (4.1.0) lib/action_dispatch/middleware/reloader.rb:73:in call' actionpack (4.1.0) lib/action_dispatch/middleware/remote_ip.rb:76:incall'
actionpack (4.1.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in call' actionpack (4.1.0) lib/action_dispatch/middleware/show_exceptions.rb:30:incall'
railties (4.1.0) lib/rails/rack/logger.rb:38:in call_app' railties (4.1.0) lib/rails/rack/logger.rb:20:inblock in call'
activesupport (4.1.0) lib/active_support/tagged_logging.rb:68:in block in tagged' activesupport (4.1.0) lib/active_support/tagged_logging.rb:26:intagged'
activesupport (4.1.0) lib/active_support/tagged_logging.rb:68:in tagged' railties (4.1.0) lib/rails/rack/logger.rb:20:incall'
quiet_assets (1.0.2) lib/quiet_assets.rb:18:in call_with_quiet_assets' actionpack (4.1.0) lib/action_dispatch/middleware/request_id.rb:21:incall'
rack (1.5.2) lib/rack/methodoverride.rb:21:in call' rack (1.5.2) lib/rack/runtime.rb:17:incall'
activesupport (4.1.0) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in call' rack (1.5.2) lib/rack/lock.rb:17:incall'
actionpack (4.1.0) lib/action_dispatch/middleware/static.rb:64:in call' rack (1.5.2) lib/rack/sendfile.rb:112:incall'
railties (4.1.0) lib/rails/engine.rb:514:in call' railties (4.1.0) lib/rails/application.rb:144:incall'
unicorn (4.8.2) lib/unicorn/http_server.rb:572:in process_client' unicorn (4.8.2) lib/unicorn/http_server.rb:666:inworker_loop'
unicorn (4.8.2) lib/unicorn/http_server.rb:521:in spawn_missing_workers' unicorn (4.8.2) lib/unicorn/http_server.rb:140:instart'
unicorn (4.8.2) bin/unicorn_rails:209:in <top (required)>' /Users/pete/.rvm/gems/ruby-2.1.1/bin/unicorn_rails:23:inload'
/Users/pete/.rvm/gems/ruby-2.1.1/bin/unicorn_rails:23:in <top (required)>' ruby-debug-ide (0.4.22) lib/ruby-debug-ide.rb:86:indebug_load'
ruby-debug-ide (0.4.22) lib/ruby-debug-ide.rb:86:in debug_program' ruby-debug-ide (0.4.22) bin/rdebug-ide:110:in<top (required)>'
-e:1:in load' -e:1:in

'

:json is a symbol instead of a string. I have no idea why. So as an experiment I hacked the core file to first check whether the value responds to valid_encoding? -- if it doesn't, then it skips it. When I make that change things work better (obviously that is a short term hack, the real solution is to figure out why the symbol is being passed). But that brings us to the second problem.

If the above issues weren't happening, then it redirects successfully to the google login screen, where it shows all the google identities I have a cookie for. I select my main one that I know I'm logged into and it redirects me to:

http://localhost:5000/omniauth/failure?message=invalid_credentials&origin=http%3A%2F%2Flocalhost%3A5000%2F&strategy=google_oauth2

But when it tries that, rails throws a Routing Error. /omniauth/failure isn't the right route, it's supposed to be /api/v1/auth/failure.

So If I could fix the redirect so it hit the correct route, then I'd see an erroneous message about invalid credentials. I'm hoping you can help me untangle this!

@lynndylanhurley
Copy link
Owner

Yikes!

  • Which versions of devise are you using?
  • Can you post the code that you're using to mount the routes?
  • Is there anything going on with devise / omniauth outside of this app that may be causing conflicts?

@PeteMichaud
Copy link
Author

Hey Lynn:

Devise is 3.2.4
Routing code:

require 'sidekiq/web'

Gaia::Application.routes.draw do


  namespace :admin do
    mount Sidekiq::Web, at: '/queue'
  end

  constraints(id: Rails.application.config.uuid_regex) do
    namespace :api, defaults: {format: :json} do
      namespace :v1 do

        mount_devise_token_auth_for 'User', at: '/auth'

        resources :users, except: [:new, :edit], constraints: {id: /[a-z0-9-]+/} do
          resources :galleries, except: [:new, :edit]
        end

        get :'search/:q', controller: :search, action: :search

      end
    end
  end

  root to: 'home#index'

end

No conflicts that I can think of, this is a pretty new project that's bonehead basic right now, nothing really fancy going on.

@lynndylanhurley
Copy link
Owner

Ok I think I see the problem. Devise doesn't seem to handle namespaces very well. Try removing this line from within the namespace blocks:

mount_devise_token_auth_for 'User', at: '/auth'

And then provide the entire namespace explicitly like this:

mount_devise_token_auth_for 'User', at: '/api/v1/auth'

So that the complete file looks like this:

require 'sidekiq/web'

Gaia::Application.routes.draw do

  mount_devise_token_auth_for 'User', at: '/api/v1/auth'

  namespace :admin do
    mount Sidekiq::Web, at: '/queue'
  end

  constraints(id: Rails.application.config.uuid_regex) do
    namespace :api, defaults: {format: :json} do
      namespace :v1 do

        resources :users, except: [:new, :edit], constraints: {id: /[a-z0-9-]+/} do
          resources :galleries, except: [:new, :edit]
        end

        get :'search/:q', controller: :search, action: :search

      end
    end
  end

  root to: 'home#index'

end

@lynndylanhurley
Copy link
Owner

@PeteMichaud - if this works, let me know and I'll add a note in the README.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants