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

Cannot use this gem alongside Devise #192

Closed
robypez opened this issue Mar 18, 2015 · 25 comments
Closed

Cannot use this gem alongside Devise #192

robypez opened this issue Mar 18, 2015 · 25 comments

Comments

@robypez
Copy link

robypez commented Mar 18, 2015

Hello. I cannot find a way to use this gem.
I'm working on a Ionic app and I need Angular authentication. I don't need Omniauth, mail and password login. Very simple.

I follow every step in the installation. I have a working Rails 4.1 app with Device installed and I'm using the beta 8 of Devise_token_auth.

I modify the migration file because I need only a couple of fields:

== 20150318160543 DeviseTokenAuthCreateUsers: migrating =======================
-- add_column(:users, :tokens, :text)
   -> 0.0014s
-- add_column(:users, :uid, :string, {:null=>false, :default=>""})
   -> 0.0439s
== 20150318160543 DeviseTokenAuthCreateUsers: migrated (0.0455s) ==============

In my user.rb I have

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :confirmable, :validatable

  include DeviseTokenAuth::Concerns::User   

In my api base controller I have

module Api
  class BaseController < ApplicationController
    include DeviseTokenAuth::Concerns::SetUserByToken
    respond_to :json
  end
end

In my routes I have

namespace :api do
    namespace :v1 do
      mount_devise_token_auth_for 'User', at: 'auth', skip: [:omniauth_callbacks]
      resources :contents, only: [:index, :show] do
        get :hottest, on: :collection
      end
    end
  end

The result is

uninitialized constant ActionDispatch::Routing::Mapper::Scope

A brief question: how can I secure only update, post and delete method in my api?

@milesmatthias
Copy link
Contributor

@robypez can you post more of the error call stack? Also, have you tried using scope :v1 instead of namespace :v1?

@robypez
Copy link
Author

robypez commented Mar 18, 2015

@milesmatthias

I try also with scope.

Here is the full stack

devise_token_auth (0.1.32.beta8) lib/devise_token_auth/rails/routes.rb:37:in `block in mount_devise_token_auth_for'
devise_token_auth (0.1.32.beta8) lib/devise_token_auth/rails/routes.rb:77:in `unnest_namespace'
devise_token_auth (0.1.32.beta8) lib/devise_token_auth/rails/routes.rb:32:in `mount_devise_token_auth_for'
config/routes.rb:236:in `block (3 levels) in <top (required)>'
actionpack (4.1.0) lib/action_dispatch/routing/mapper.rb:805:in `block in namespace'
actionpack (4.1.0) lib/action_dispatch/routing/mapper.rb:740:in `scope'
actionpack (4.1.0) lib/action_dispatch/routing/mapper.rb:805:in `namespace'
actionpack (4.1.0) lib/action_dispatch/routing/mapper.rb:1373:in `namespace'
config/routes.rb:235:in `block (2 levels) in <top (required)>'
actionpack (4.1.0) lib/action_dispatch/routing/mapper.rb:805:in `block in namespace'
actionpack (4.1.0) lib/action_dispatch/routing/mapper.rb:740:in `scope'
actionpack (4.1.0) lib/action_dispatch/routing/mapper.rb:805:in `namespace'
actionpack (4.1.0) lib/action_dispatch/routing/mapper.rb:1373:in `namespace'
config/routes.rb:234:in `block in <top (required)>'
actionpack (4.1.0) lib/action_dispatch/routing/route_set.rb:335:in `instance_exec'
actionpack (4.1.0) lib/action_dispatch/routing/route_set.rb:335:in `eval_block'
actionpack (4.1.0) lib/action_dispatch/routing/route_set.rb:313:in `draw'
config/routes.rb:3:in `<top (required)>'
railties (4.1.0) lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
railties (4.1.0) lib/rails/application/routes_reloader.rb:40:in `each'
railties (4.1.0) lib/rails/application/routes_reloader.rb:40:in `load_paths'
railties (4.1.0) lib/rails/application/routes_reloader.rb:16:in `reload!'
railties (4.1.0) lib/rails/application/routes_reloader.rb:26:in `block in updater'
activesupport (4.1.0) lib/active_support/file_update_checker.rb:75:in `call'
activesupport (4.1.0) lib/active_support/file_update_checker.rb:75:in `execute'
railties (4.1.0) lib/rails/application/routes_reloader.rb:7:in `execute'
railties (4.1.0) lib/rails/application/finisher.rb:83:in `block (2 levels) in <module:Finisher>'
activesupport (4.1.0) lib/active_support/callbacks.rb:438:in `instance_exec'
activesupport (4.1.0) lib/active_support/callbacks.rb:438:in `block in make_lambda'
activesupport (4.1.0) lib/active_support/callbacks.rb:184:in `call'
activesupport (4.1.0) lib/active_support/callbacks.rb:184:in `block in simple'
activesupport (4.1.0) lib/active_support/callbacks.rb:185:in `call'
activesupport (4.1.0) lib/active_support/callbacks.rb:185:in `block in simple'
activesupport (4.1.0) lib/active_support/callbacks.rb:185:in `call'
activesupport (4.1.0) lib/active_support/callbacks.rb:185:in `block in simple'
activesupport (4.1.0) lib/active_support/callbacks.rb:185:in `call'
activesupport (4.1.0) lib/active_support/callbacks.rb:185:in `block in simple'
activesupport (4.1.0) lib/active_support/callbacks.rb:86:in `call'
activesupport (4.1.0) lib/active_support/callbacks.rb:86:in `run_callbacks'
actionpack (4.1.0) lib/action_dispatch/middleware/reloader.rb:83:in `prepare!'
actionpack (4.1.0) lib/action_dispatch/middleware/reloader.rb:71:in `call'
actionpack (4.1.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
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:in `call'
railties (4.1.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.0) lib/rails/rack/logger.rb:20:in `block 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:in `tagged'
activesupport (4.1.0) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.0) lib/rails/rack/logger.rb:20:in `call'
quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
request_store (1.1.0) lib/request_store/middleware.rb:8:in `call'
actionpack (4.1.0) 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.0) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack-rewrite (1.5.1) lib/rack/rewrite.rb:24:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.0) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.0) lib/rails/engine.rb:514:in `call'
railties (4.1.0) lib/rails/application.rb:144:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
puma (2.11.1) lib/puma/server.rb:507:in `handle_request'
puma (2.11.1) lib/puma/server.rb:375:in `process_client'
puma (2.11.1) lib/puma/server.rb:262:in `block in run'
puma (2.11.1) lib/puma/thread_pool.rb:104:in `call'
puma (2.11.1) lib/puma/thread_pool.rb:104:in `block in spawn_thread'
This error occurred while loading the following files:
   /Users/scrivoaroby/Desktop/lavori/dday/config/routes.rb

@milesmatthias
Copy link
Contributor

Looks like this line is broken. You're using the 0.1.32.beta8 version, in which this may be a bug.

I'm using version 0.1.29 successfully side by side with devise 3.2.4 in my Rails 4.1.8 app. Maybe specify a non-beta version and try again?

@robypez
Copy link
Author

robypez commented Mar 18, 2015

Using 1.30 I have another problem:

could not load 'omniauth'. Please ensure you have the omniauth gem >= 1.0.0 installed and listed in your Gemfile.

@milesmatthias
Copy link
Contributor

Looks like you don't have the omniauth gem installed... is it in the Gemfile? Did you do bundle install?

@robypez
Copy link
Author

robypez commented Mar 18, 2015

@milesmatthias I want to use it without omniauth.

I followed the instructions to disable it

@robypez
Copy link
Author

robypez commented Mar 18, 2015

I can add another info:

using 1.30 with omniauth gem it works

@milesmatthias
Copy link
Contributor

I think @lynndylanhurley might have some comments here. Personally, it seems like an overreach of this gem to require omniauth, but maybe there's some other thinking there.

@robypez
Copy link
Author

robypez commented Mar 19, 2015

Yes, I think is a bug.

@robypez
Copy link
Author

robypez commented Mar 19, 2015

@lynndylanhurley @milesmatthias another info.

My routes.rb

devise_for :users, controllers: { registrations: 'users/registrations', confirmations: 'users/confirmations', sessions: 'users/sessions' }
  # for step1 of registration
  devise_scope :user do
    post "/users/registrations/validate_attributes" => "users/registrations#validate_attributes"
  end

  resources :users, only: [:update] do
    collection do
      get :ask_email
      post :complete_sign_up
    end
    member do
      get 'associate_password'
    end
  end

namespace :api do
    scope :v1 do
      mount_devise_token_auth_for 'User', at: 'auth', skip: [:omniauth_callbacks]
    end
    namespace :v1 do
      resources :contents, only: [:index, :show] do
        get :hottest, on: :collection
      end
    end
  end

@lynndylanhurley
Copy link
Owner

Thanks guys, I think this is a bug. I'll look into the problem tonight.

@robypez
Copy link
Author

robypez commented Mar 19, 2015

@lynndylanhurley thank you. If you need test or support I'm here! 👍

@robypez
Copy link
Author

robypez commented Mar 20, 2015

@lynndylanhurley for you info I cloned the gem in local and I comment the line where you clear the scope:


# clear scope so controller routes aren't namespaced
        # @scope = ActionDispatch::Routing::Mapper::Scope.new(
        #   path:         "",
        #   shallow_path: "",
        #   constraints:  {},
        #   defaults:     {},
        #   options:      {},
        #   parent:       nil
        # )

Now rake routes works but of course the result is:


new_api_v1_user_session GET    /api/v1/auth/sign_in(.:format)                                                          devise_token_auth/sessions#new
                        api_v1_user_session POST   /api/v1/auth/sign_in(.:format)                                                          devise_token_auth/sessions#create
                destroy_api_v1_user_session DELETE /api/v1/auth/sign_out(.:format)                                                         devise_token_auth/sessions#destroy
                       api_v1_user_password POST   /api/v1/auth/password(.:format)                                                         devise_token_auth/passwords#create
                   new_api_v1_user_password GET    /api/v1/auth/password/new(.:format)                                                     devise_token_auth/passwords#new
                  edit_api_v1_user_password GET    /api/v1/auth/password/edit(.:format)                                                    devise_token_auth/passwords#edit
                                            PATCH  /api/v1/auth/password(.:format)                                                         devise_token_auth/passwords#update
                                            PUT    /api/v1/auth/password(.:format)                                                         devise_token_auth/passwords#update
            cancel_api_v1_user_registration GET    /api/v1/auth/cancel(.:format)                                                           devise_token_auth/registrations#cancel
                   api_v1_user_registration POST   /api/v1/auth(.:format)                                                                  devise_token_auth/registrations#create
               new_api_v1_user_registration GET    /api/v1/auth/sign_up(.:format)                                                          devise_token_auth/registrations#new
              edit_api_v1_user_registration GET    /api/v1/auth/edit(.:format)                                                             devise_token_auth/registrations#edit
                                            PATCH  /api/v1/auth(.:format)                                                                  devise_token_auth/registrations#update
                                            PUT    /api/v1/auth(.:format)                                                                  devise_token_auth/registrations#update
                                            DELETE /api/v1/auth(.:format)                                                                  devise_token_auth/registrations#destroy
                   api_v1_user_confirmation POST   /api/v1/auth/confirmation(.:format)                                                     devise_token_auth/confirmations#create
               new_api_v1_user_confirmation GET    /api/v1/auth/confirmation/new(.:format)                                                 devise_token_auth/confirmations#new
                                            GET    /api/v1/auth/confirmation(.:format)                                                     devise_token_auth/confirmations#show
          api_v1_api_v1_auth_validate_token GET    /api/v1/api/v1/auth/validate_token(.:format)                                            api/v1/devise_token_auth/token_validations#validate_token

As you can see here I have the double namespace.

PS: is there a way to have just login routes (no registration, update, etc).

@robypez
Copy link
Author

robypez commented Mar 20, 2015

@lynndylanhurley

The problem is Rails 4.1: Scope.new works only in Rails 4.2.
By the way I cannot upgrade to Rails 4.2, I must use Rails 4.1.x.

Using Rails 4.1 and Devise Token Auth 1.30 is not possible to delete unused routes.

mount_devise_token_auth_for 'User', at: 'auth', skip: [:omniauth_callbacks, :registrations, :passwords]

doesn't work.

@papayaah
Copy link

been awhile - still broken on latest master 8a6ba64

@d3crypt3d
Copy link

I'm having the same issue with Rails 4.1.1 and devise_token_auth 0.1.37

@arinhouck
Copy link

arinhouck commented May 18, 2016

I am also experiencing this issue on Rails 4.1.8 and devise_token_auth 0.1.37. I updated from 0.1.30 to fix the bcrypt error then got this error.

/Users/arinhouck/.rvm/gems/ruby-2.2.4/gems/devise_token_auth-0.1.37/lib/devise_token_auth/rails/routes.rb:40:in `block in mount_devise_token_auth_for': uninitialized constant ActionDispatch::Routing::Mapper::Scope (NameError)
    from /Users/arinhouck/.rvm/gems/ruby-2.2.4/gems/devise_token_auth-0.1.37/lib/devise_token_auth/rails/routes.rb:86:in `unnest_namespace'
    from /Users/arinhouck/.rvm/gems/ruby-2.2.4/gems/devise_token_auth-0.1.37/lib/devise_token_auth/rails/routes.rb:32:in `mount_devise_token_auth_for'
  namespace :api do
    scope :v1 do
      mount_devise_token_auth_for 'User', at: 'auth', controllers: {
        sessions: 'overrides/sessions'
      }
    end
    namespace :v1 do
      resources :posts
    end
  end

@aliceclv
Copy link

aliceclv commented Jun 9, 2016

Using rails -> 4.8.1 and the latest version of device_token_auth, I've also this error when running rake routes:

rake aborted!
NameError: uninitialized constant ActionDispatch::Routing::Mapper::Scope

With the following:

Rails.application.routes.draw do
  namespace :api_v1, path: :v1, defaults: { format: :json } do
    # Authentication
    mount_devise_token_auth_for 'Customer', at: 'auth'
  end
end

@BecomingSam
Copy link

@aliceclv I got the same error when running rake db:migrate. Did you fix yours?

@aliceclv
Copy link

@BecomingSam I've given up using the gem and followed a tutorial to implement the API authentication system myself: you can find it here

@Alirun
Copy link

Alirun commented Aug 3, 2016

@aliceclv Thanks for the link. I'll do the same.

@leolopelofranco
Copy link

Has this been ever resolved?

@tyronewilson
Copy link

@aliceclv for the amount of effort in "roll your own" wouldn't it be possible to fix and pull request?

@mariohmol
Copy link

same issue =(

@zachfeldman
Copy link
Contributor

Hi there @robypez ,

In an effort to cleanup this project and prioritize a bit, we're marking issues that haven't had any activity in a while with a "close-in-7-days" label. If we don't hear from you in about a week, we'll be closing this issue. Obviously feel free to re-open it at any time if it's the right time or this was done in error!

If you are still having the issue (especially if it's a bug report) please refer to our new Issue Template to provide some more details to help us solve it.

Hope all is well.

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

No branches or pull requests