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

Routes not properly set #101

Closed
c0mrade opened this issue Jan 5, 2015 · 18 comments
Closed

Routes not properly set #101

c0mrade opened this issue Jan 5, 2015 · 18 comments

Comments

@c0mrade
Copy link

c0mrade commented Jan 5, 2015

I just realized that this might be a duplicate of #96

I'm not sure is this a rails thing or a devise token auth thing but here is the part of routes in my project :

namespace :api, defaults: { format: :json } do
    scope :v1 do
      mount_devise_token_auth_for 'User', at: '/auth'
      resources :users
    end
 end

Here is some of the output of rake routes:

  new_api_user_session GET      /api/v1/auth/sign_in(.:format)            devise_token_auth/sessions#new    {:format=>:json}
  api_user_session POST     /api/v1/auth/sign_in(.:format)            devise_token_auth/sessions#create {:format=>:json}
  destroy_api_user_session DELETE   /api/v1/auth/sign_out(.:format)              devise_token_auth/sessions#destroy {:format=>:json}

Which is generated to work with my routes. However token validation routes are not generating correctly:

  api_validate_token GET      /api/v1/auth/validate_token(.:format)       api/devise_token_auth/token_validations#validate_token {:format=>:json}
  api_failure GET      /api/v1/auth/failure(.:format)            api/devise_token_auth/omniauth_callbacks#omniauth_failure {:format=>:json}
  api GET      /api/v1/auth/:provider/callback(.:format) api/devise_token_auth/omniauth_callbacks#omniauth_success {:format=>:json}

So every time I check my token using this url /api/v1/auth/validate_token I get this error:

  Started GET "/api/v1/auth/validate_token" for 127.0.0.1 at 2015-01-04 19:06:49 -0500

  ActionController::RoutingError (uninitialized constant Api::V1::DeviseTokenAuth):

This is because the routes are generated incorrectly again output of rake routes :

  api_validate_token GET      /api/v1/auth/validate_token(.:format)     api/devise_token_auth/token_validations#validate_token {:format=>:json}

I'm using the latest version devise_token_auth (0.1.31.beta1), is there any work around for this?

@c0mrade
Copy link
Author

c0mrade commented Jan 5, 2015

Thanks for the fix @lynndylanhurley

The routes are drawn as they should now when the mount_devise_token_auth_for is inside the scope (I upgraded to the latest version at this time 0.1.31.beta2) i.e :

  namespace :api, defaults: { format: :json } do
      scope :v1 do
        mount_devise_token_auth_for 'User', at: '/auth'
        resources :users
     end
 end

However there are still issues when the mount_devise_token_auth_for is inside the scope like above. When trying to validate token at this path I get the error about uninitialized constant like below:

  Started GET "/api/v1/auth/validate_token" for 127.0.0.1 at 2015-01-04 22:30:11 -0500
  ActionController::RoutingError (uninitialized constant Api::DeviseTokenAuth):

However if I place my routes code below the namespace like this:

  namespace :api, defaults: { format: :json } do
      scope :v1 do
        resources :users
     end
 end

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

The validate token hits the right controller but the both requests have the same path:

  Started GET "/api/v1/auth/validate_token" for 127.0.0.1 at 2015-01-04 22:38:44 -0500
  Processing by DeviseTokenAuth::TokenValidationsController#validate_token

@lynndylanhurley
Copy link
Owner

Thanks @c0mrade, I'm looking into this now.

@c0mrade
Copy link
Author

c0mrade commented Jan 5, 2015

thanks @lynndylanhurley, let me know if I can help with anything else, I'll probably use this gem along with ng-token-auth in my app, not started yet though. Just trying stuff out at the moment

@lynndylanhurley
Copy link
Owner

Ok this one was more difficult than I'd realized. I think version 0.1.31.beta5 contains the fix. Please test to confirm.

@c0mrade
Copy link
Author

c0mrade commented Jan 5, 2015

Hello @lynndylanhurley

Now I'm having another trouble logging in, here is the that bit from logs:

  Started POST "/api/v1/auth/sign_in" for 127.0.0.1 at 2015-01-05 09:18:45 -0500
  Processing by DeviseTokenAuth::SessionsController#create as JSON
  Parameters: {"email"=>"xxxx@gmail.com", "password"=>"[FILTERED]", "session"=>{"email"=>"xxxx@gmail.com", "password"=>"[FILTERED]"}}
  Unpermitted parameters: format, session
  User Load (18.0ms)  SELECT  "users".* FROM "users" WHERE (uid='xxxx@gmail.com' AND provider='email')  ORDER BY "users"."id" ASC LIMIT 1
  Unpermitted parameters: format, session
  Unpermitted parameters: format, session 
  Unpermitted parameters: format, session
  Completed 500 Internal Server Error in 250ms

Then continued with the error:

  IndexError (string not matched):
  `[]='
  devise_token_auth (0.1.31.beta5) app/controllers/devise_token_auth/sessions_controller.rb:29:in   `create'
  actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  actionpack (4.2.0) lib/abstract_controller/base.rb:198:in `process_action'
  actionpack (4.2.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
  actionpack (4.2.0) lib/abstract_controller/callbacks.rb:20:in `process_action'

Not sure if this helps but this is what happens when I key in the wrong password :

  ActiveRecord::SerializationTypeMismatch (Attribute was supposed to be a   ActiveSupport::HashWithIndifferentAccess, but was a String. --
  "{\"p0RTcSN3ZCHYaO5TpQI1Uw\":{\"token\":\"$2a$10$ypSd/7aN0BetBu3d6LoSs.TSu5WTBQ9a9EDRiN/JF5eHnqUHtnj8K\",
  \"expiry\":1421638207},\"BO4CcAjyaoCVjoSVMt6RYA\":
  {\"token\":\"$2a$10$hRQQKE/yvMswb5TYcC9vPuIYll8nMS7o0F71mfHdpBDqrO6SNZR6i\",\"expiry\":1421638404}}"):
  activerecord (4.2.0) lib/active_record/coders/yaml_column.rb:18:in `dump'
  activerecord (4.2.0) lib/active_record/type/serialized.rb:32:in `changed_in_place?'
  activerecord (4.2.0) lib/active_record/attribute.rb:54:in `changed_in_place_from?'
  activerecord (4.2.0) lib/active_record/attribute_methods/dirty.rb:74:in `attribute_changed_in_place?'
  activerecord (4.2.0) lib/active_record/attribute_methods/dirty.rb:149:in `changed_in_place'

@lynndylanhurley
Copy link
Owner

Thanks @c0mrade - I'll get this resolved ASAP.

@lynndylanhurley
Copy link
Owner

@c0mrade - I just pushed version 0.1.31.beta6, and I think these issues have been resolved. Please confirm.

@jirikolarik
Copy link

I have the same problem with 0.1.31.beta8

Unpermitted parameters: format, session

@lynndylanhurley
Copy link
Owner

@jirikolarik - that looks like a different issue. Please open a new issue, and include the post body from the failed request.

@c0mrade
Copy link
Author

c0mrade commented Jan 12, 2015

Hello @lynndylanhurley

When my routes are defined like this :

  namespace :api, defaults: { format: :json } do
      scope :v1 do
        mount_devise_token_auth_for 'User', at: '/auth'
        resources :users
      end
    end

And I refresh a page, triggering the validate token, this is the error I get:

  Started GET "/api/v1/auth/validate_token" for 127.0.0.1 at 2015-01-11 21:38:33 -0500
  Processing by DeviseTokenAuth::TokenValidationsController#validate_token as HTML
  Completed 500 Internal Server Error in 3ms

  NoMethodError (undefined method `name' for nil:NilClass):
    devise (3.4.1) app/controllers/devise_controller.rb:22:in `resource_name'
    devise_token_auth (0.1.31.beta9) app/controllers/devise_token_auth/application_controller.rb:10:in       `resource_class'
   devise_token_auth (0.1.31.beta9)       app/controllers/devise_token_auth/concerns/set_user_by_token.rb:19:in `set_user_by_token'
   activesupport (4.2.0) lib/active_support/callbacks.rb:427:in `make_lambda'

However when I change my route to :

When my routes are defined like this :

  namespace :api, defaults: { format: :json } do
      scope :v1 do
        resources :users
      end
    end

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

This works without issues. So there are some more issues I guess. Thanks

@lynndylanhurley
Copy link
Owner

Can you try changing this line:

mount_devise_token_auth_for 'User', at: '/auth'

to this:

mount_devise_token_auth_for 'User', at: 'auth'

(Remove the "/" from "/auth")

@c0mrade
Copy link
Author

c0mrade commented Jan 12, 2015

@lynndylanhurley

already tried it, sorry should have mentioned it. Result is the same. Using the latest version devise_token_auth (0.1.31.beta9)

@lynndylanhurley
Copy link
Owner

That's strange, the test-case is working fine using nearly identical code:

Also, several others have reported that it's working in the latest beta. Did you restart your server and everything?

I'll keep testing to see if I can reproduce.

@c0mrade
Copy link
Author

c0mrade commented Jan 12, 2015

@lynndylanhurley
I think I did, but I'll retest today just in case.

@jhblacklock
Copy link

@lynndylanhurley,

I came across the same issue in 0.1.32.beta1. After testing I found that commenting out the following in token_validations_controller_test.rb

# define :users as the first devise mapping:
# mount_devise_token_auth_for 'User', at: 'auth'

Caused the namespaced test to fail.

In my current project, I mounted the unscoped/namespaced engine which resolved the error.

mount_devise_token_auth_for 'User', at: 'auth'

namespace :api, defaults: { format: 'json' } do
  scope :v1 do
    mount_devise_token_auth_for 'User', at: 'auth'
    get '/greet' => 'pages#greet', as: 'greet'
  end
end

I hope this helps. I will spend some more time debugging.

Thanks!

@Vanuan
Copy link

Vanuan commented Mar 17, 2015

What about making "devise_token_auth" act more like a library than a drop-in solution?
Router is getting more and more complicated.

@zachfeldman
Copy link
Contributor

Hi there @c0mrade ,

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.

@c0mrade
Copy link
Author

c0mrade commented Oct 7, 2017

this one is good to close too

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

6 participants