lynndylanhurley / devise_token_auth

Token based authentication for Rails JSON APIs. Designed to work with jToker and ng-token-auth.
Do What The F*ck You Want To Public License
3.55k stars 1.14k forks source link

Mount in multiple namespaces - not supported? #1235

Open caseyprovost opened 6 years ago

caseyprovost commented 6 years ago

Hi there!

The problem I am having hopefully is a simple one. I am attempting to mount devise_token_auth a second time in the v2 namespace, but cannot get it to allow me to do so. It is pretty important since we are starting the rebuild of our API and need auth changed a bit. Is this a bug or a known constraint of the library?

Routes
scope '/v2', module: :v2, constraints: ApiConstraints.new(version: 2, default: false), defaults: { format: :jsonapi } do
    mount_devise_token_auth_for 'User', at: 'auth', controllers: {
      sessions: 'v2/sessions',
      omniauth_callbacks: 'v2/devise_token_auth/omniauth_callbacks'
    }

    resources :offers
  end

 scope '/v1', module: :v1, constraints: ApiConstraints.new(version: 1, default: false) do
    mount_devise_token_auth_for 'User', at: 'auth', controllers: {
      sessions: 'v1/sessions',
      omniauth_callbacks: 'v1/devise_token_auth/omniauth_callbacks'
    }
    resources :accounting_codes, only: [:index, :show]
 end

Gem Details

Stack Trace

/Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/route_set.rb:584:in `add_route': Invalid route name, already in use: 'new_user_session'  (ArgumentError)
You may have defined two routes with the same name using the `:as` option, or you may be overriding a route already defined by a resource with the same naming. For the latter, you can restrict the routes created with `resources` as explained here: 
http://guides.rubyonrails.org/routing.html#restricting-the-routes-created
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/mapper.rb:1933:in `add_route'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/mapper.rb:1904:in `decomposed_match'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/mapper.rb:1902:in `block in decomposed_match'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/mapper.rb:1517:in `block (2 levels) in member'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/mapper.rb:1833:in `path_scope'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/mapper.rb:1517:in `block in member'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/mapper.rb:1713:in `with_scope_level'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/mapper.rb:1511:in `member'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/mapper.rb:1902:in `decomposed_match'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/mapper.rb:1873:in `block in map_match'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/mapper.rb:1871:in `each'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/mapper.rb:1871:in `map_match'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/mapper.rb:1610:in `match'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/mapper.rb:743:in `map_method'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/mapper.rb:704:in `get'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/devise-4.4.3/lib/devise/rails/routes.rb:379:in `block in devise_session'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/mapper.rb:1290:in `block (2 levels) in resource'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/mapper.rb:1721:in `block in resource_scope'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/mapper.rb:894:in `controller'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/mapper.rb:1721:in `resource_scope'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/mapper.rb:1289:in `block in resource'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/mapper.rb:1713:in `with_scope_level'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/mapper.rb:1287:in `resource'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/devise-4.4.3/lib/devise/rails/routes.rb:378:in `devise_session'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/devise-4.4.3/lib/devise/rails/routes.rb:268:in `block (4 levels) in devise_for'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/devise-4.4.3/lib/devise/rails/routes.rb:268:in `each'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/devise-4.4.3/lib/devise/rails/routes.rb:268:in `block (3 levels) in devise_for'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/devise-4.4.3/lib/devise/rails/routes.rb:472:in `with_devise_exclusive_scope'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/devise-4.4.3/lib/devise/rails/routes.rb:267:in `block (2 levels) in devise_for'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/devise-4.4.3/lib/devise/rails/routes.rb:370:in `block in devise_scope'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/mapper.rb:1008:in `block in constraints'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/mapper.rb:879:in `scope'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/mapper.rb:1008:in `constraints'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/devise-4.4.3/lib/devise/rails/routes.rb:369:in `devise_scope'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/devise-4.4.3/lib/devise/rails/routes.rb:266:in `block in devise_for'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/devise-4.4.3/lib/devise/rails/routes.rb:242:in `each'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/devise-4.4.3/lib/devise/rails/routes.rb:242:in `devise_for'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/devise_token_auth-0.2.0/lib/devise_token_auth/rails/routes.rb:30:in `mount_devise_token_auth_for'
    from /Users/casey/Sites/aeg/aeg-prince-api/config/routes.rb:31:in `block (2 levels) in <top (required)>'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/mapper.rb:879:in `scope'
    from /Users/casey/Sites/aeg/aeg-prince-api/config/routes.rb:30:in `block in <top (required)>'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/route_set.rb:432:in `instance_exec'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/route_set.rb:432:in `eval_block'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/route_set.rb:414:in `draw'
    from /Users/casey/Sites/aeg/aeg-prince-api/config/routes.rb:3:in `<top (required)>'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:281:in `load'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:281:in `block in load'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:253:in `load_dependency'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:281:in `load'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/railties-5.2.1/lib/rails/application/routes_reloader.rb:41:in `block in load_paths'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/railties-5.2.1/lib/rails/application/routes_reloader.rb:41:in `each'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/railties-5.2.1/lib/rails/application/routes_reloader.rb:41:in `load_paths'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/railties-5.2.1/lib/rails/application/routes_reloader.rb:20:in `reload!'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/railties-5.2.1/lib/rails/application/routes_reloader.rb:30:in `block in updater'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/activesupport-5.2.1/lib/active_support/file_update_checker.rb:83:in `execute'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/railties-5.2.1/lib/rails/application/routes_reloader.rb:10:in `execute'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/railties-5.2.1/lib/rails/application/finisher.rb:130:in `block in <module:Finisher>'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/railties-5.2.1/lib/rails/initializable.rb:32:in `instance_exec'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/railties-5.2.1/lib/rails/initializable.rb:32:in `run'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/railties-5.2.1/lib/rails/initializable.rb:61:in `block in run_initializers'
    from /Users/casey/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/tsort.rb:228:in `block in tsort_each'
    from /Users/casey/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
    from /Users/casey/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/tsort.rb:431:in `each_strongly_connected_component_from'
    from /Users/casey/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/tsort.rb:349:in `block in each_strongly_connected_component'
    from /Users/casey/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/tsort.rb:347:in `each'
    from /Users/casey/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/tsort.rb:347:in `call'
    from /Users/casey/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/tsort.rb:347:in `each_strongly_connected_component'
    from /Users/casey/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/tsort.rb:226:in `tsort_each'
    from /Users/casey/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/tsort.rb:205:in `tsort_each'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/railties-5.2.1/lib/rails/initializable.rb:60:in `run_initializers'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/railties-5.2.1/lib/rails/application.rb:361:in `initialize!'
    from /Users/casey/Sites/aeg/aeg-prince-api/config/environment.rb:5:in `<top (required)>'
    from config.ru:3:in `require_relative'
    from config.ru:3:in `block in <main>'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/rack-2.0.5/lib/rack/builder.rb:55:in `instance_eval'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/rack-2.0.5/lib/rack/builder.rb:55:in `initialize'
    from config.ru:in `new'
    from config.ru:in `<main>'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/rack-2.0.5/lib/rack/builder.rb:49:in `eval'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/rack-2.0.5/lib/rack/builder.rb:49:in `new_from_string'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/rack-2.0.5/lib/rack/builder.rb:40:in `parse_file'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/rack-2.0.5/lib/rack/server.rb:319:in `build_app_and_options_from_config'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/rack-2.0.5/lib/rack/server.rb:219:in `app'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/railties-5.2.1/lib/rails/commands/server/server_command.rb:27:in `app'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/rack-2.0.5/lib/rack/server.rb:354:in `wrapped_app'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/railties-5.2.1/lib/rails/commands/server/server_command.rb:89:in `log_to_stdout'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/railties-5.2.1/lib/rails/commands/server/server_command.rb:51:in `start'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/railties-5.2.1/lib/rails/commands/server/server_command.rb:147:in `block in perform'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/railties-5.2.1/lib/rails/commands/server/server_command.rb:142:in `tap'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/railties-5.2.1/lib/rails/commands/server/server_command.rb:142:in `perform'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/thor-0.20.0/lib/thor/command.rb:27:in `run'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/thor-0.20.0/lib/thor/invocation.rb:126:in `invoke_command'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/thor-0.20.0/lib/thor.rb:387:in `dispatch'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/railties-5.2.1/lib/rails/command/base.rb:65:in `perform'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/railties-5.2.1/lib/rails/command.rb:46:in `invoke'
    from /Users/casey/.rvm/gems/ruby-2.4.1/gems/railties-5.2.1/lib/rails/commands.rb:18:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'
MaicolBen commented 6 years ago

@caseyprovost I thinik that way doesn't work, you should move mount_devise_token_auth_for outside the scope, and define 2 with different at, eg: at: '/api/v1/auth' and at: '/api/v2/auth'

caseyprovost commented 6 years ago

I gave this a whirl and am still experiencing errors.Below is my router code and result.

mount_devise_token_auth_for 'User', at: 'v1/auth', controllers: {
    sessions: 'v1/sessions',
    omniauth_callbacks: 'v1/devise_token_auth/omniauth_callbacks'
  }

  mount_devise_token_auth_for 'User', at: 'v2/auth', controllers: {
    sessions: 'v2/sessions',
    omniauth_callbacks: 'v2/devise_token_auth/omniauth_callbacks'
  }
/Users/casey/.rvm/gems/ruby-2.4.1/gems/actionpack-5.2.1/lib/action_dispatch/routing/route_set.rb:584:in `add_route': Invalid route name, already in use: 'new_user_session' 
You may have defined two routes with the same name using the `:as` option, or you may be overriding a route already defined by a resource with the same naming. For the latter, you can restrict the routes created with `resources` as explained here: 
http://guides.rubyonrails.org/routing.html#restricting-the-routes-created (ArgumentError)
caseyprovost commented 6 years ago

@MaicolBen ^^

MaicolBen commented 6 years ago

@caseyprovost You were right with your first approach (maybe there is a way to get mine working as well), as the error states, you have to define 2 different as, e.g as: 'v1'

pedrorio commented 5 years ago

For future reference, you can mount it like:

scope 'general', as: 'general' do
      mount_devise_token_auth_for 'User', at: 'auth', controllers: {
          sessions: 'general/sessions'
      }
end