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.54k stars 1.13k forks source link

DeviseTokenAuth::Concerns::SetUserByToken wrong number of arguments 1 for 0 #450

Open lolgear opened 8 years ago

lolgear commented 8 years ago

Something strange happens inside DeviseTokenAuth::Concerns::SetUserByToken#set_user_by_token()

  activesupport (4.2.5) lib/active_support/core_ext/class/attribute.rb:106:in `block (2 levels) in class_attribute'
devise_token_auth/app/controllers/devise_token_auth/concerns/set_user_by_token.rb:24:in `set_user_by_token'
  devise_token_auth/lib/devise_token_auth/controllers/helpers.rb:115:in `current_user'
  devise_token_auth/lib/devise_token_auth/controllers/helpers.rb:103:in `authenticate_user!'

It seems that rc = resource_class(mapping) doesn't work well. ( resource_class doesn't work well with parameter )

lolgear commented 8 years ago

I think that they are related: https://github.com/lynndylanhurley/devise_token_auth/issues/433

lolgear commented 8 years ago

additional info:

activesupport (4.2.5) lib/active_support/core_ext/class/attribute.rb:106:in `block (2 levels) in class_attribute'
devise_token_auth (0.1.36) app/controllers/devise_token_auth/concerns/set_user_by_token.rb:21:in `set_user_by_token'
devise_token_auth (0.1.36) lib/devise_token_auth/controllers/helpers.rb:115:in `current_user'
cancancan (1.13.1) lib/cancan/controller_additions.rb:357:in `current_ability'
cancancan (1.13.1) lib/cancan/controller_resource.rb:214:in `current_ability'
cancancan (1.13.1) lib/cancan/controller_resource.rb:72:in `load_collection?'
cancancan (1.13.1) lib/cancan/controller_resource.rb:33:in `load_resource'
cancancan (1.13.1) lib/cancan/controller_resource.rb:25:in `load_and_authorize_resource'
reaper commented 8 years ago

Got the same issue on master (9668dc77a0e9) when authenticating a user not omniauthable

config.rb

devise_for :admins, controllers: { sessions: 'admin/sessions' }
mount_devise_token_auth_for 'User', at: 'users_auth'

GET: http://localhost:3000/admins/sign_in produces:

ArgumentError - wrong number of arguments (1 for 0):
~/.rvm/gems/ruby-2.2.1/bundler/gems/devise_token_auth-9668dc77a0e9/app/controllers/devise_token_auth/concerns/set_user_by_token.rb:21:in `set_user_by_token'
lolgear commented 8 years ago

@reaper could you try my PR: #454 ?

In Gemfile:

git: 'https://github.com/lolgear/devise_token_auth', branch: 'missing_methods_for_cancan'
reaper commented 8 years ago

Yes it doesn't raise the wrong number of arguments issue, but I cannot access to the login page of the admin model

rossshannon commented 8 years ago

@lolgear The PR avoided the wrong number of arguments issue for me too, but the Devise helpers all return false (current_user, is_logged_in? etc.). I have made some progress by moving the Concern (include DeviseTokenAuth::Concerns::SetUserByToken) out of my main ApplicationController and into a namespaced API::ApplicationController (controllers/api/application_controller.rb) (suggested here):

class Api::ApplicationController < ActionController::Base
  include DeviseTokenAuth::Concerns::SetUserByToken
  protect_from_forgery with: :null_session
  respond_to :json
end

This seems to stop the concern from overriding helpers in base Devise, but it feels like there is still some incompatibilities to come though.

lolgear commented 8 years ago

@rossshannon could you try to prepare sample, that will suffer from this issue and can be fixed by your solution?

I have a sample where I try to catch this bug, but have no luck yet.

https://github.com/lolgear/devise_token_auth_demo_api_app

ltfschoen commented 8 years ago

@rossshannon Thank you! I recently added devise_token_auth gem following this guide http://www.developingandrails.com/2015/02/api-authentication-with-devisetokenauth.html. After submitting username/password it gave me the error wrong number of arguments 1 for 0 with reference to my call to the user#show route in my Users::SessionsController, which dumbfounded me, as this route should expect a parameter as defined in rake routes and as prior to installing devise_token_auth it logged in without error. The following code user_path(current_user.id) from this snippet was causing the error:

  def after_sign_in_path_for(resource)
    user_path(current_user.id)
  end

I came across this github issue and your comment dated 25 Jan. I tried your advice of creating another separate controllers/api/application_controller.rb with the contents you suggested, and removed the old code that the gem had added to controllers/application_controller.rb.

It now runs without any error :)

lsarni commented 7 years ago

I'm getting the same error when trying to use this gem in combination with ActiveAdmin:

ArgumentError - wrong number of arguments (given 1, expected 0):
  activesupport (5.1.3) lib/active_support/core_ext/class/attribute.rb:110:in `block (2 levels) in class_attribute'
  devise_token_auth (0.1.42) app/controllers/devise_token_auth/concerns/set_user_by_token.rb:21:in `set_user_by_token'
  devise_token_auth (0.1.42) lib/devise_token_auth/controllers/helpers.rb:123:in `current_admin_user'
  devise_token_auth (0.1.42) lib/devise_token_auth/controllers/helpers.rb:111:in `authenticate_admin_user!'
  activeadmin (1.1.0) lib/active_admin/base_controller.rb:39:in `authenticate_active_admin_user'

I haven't been able to make the workaround work. I have another model called User for the API side of my app and that one works just fine.

bananatron commented 5 years ago

For other folks that end up here, I think this sheds some light: https://stackoverflow.com/questions/35199766/activeadmin-with-rails-api-app-errors-argumenterror-in-admindashboardcontroll