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

NoMethodError (undefined method `name' for nil:NilClass) - devise_controller.rb:22 #134

Closed
rmagnum2002 opened this issue Feb 5, 2015 · 20 comments

Comments

@rmagnum2002
Copy link

Scenario - I log in successfully, and navigate through pages as a logged in user until I hit refresh.
Happens with this line:

# config.change_headers_on_each_request = true

commented, uncommented, set to true or false.. all cases.

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) app/controllers/devise_token_auth/application_controller.rb:10:in `resource_class'
  devise_token_auth (0.1.31) app/controllers/devise_token_auth/concerns/set_user_by_token.rb:19:in `set_user_by_token'

line 22 from devise_controller:

21  def resource_name
22    devise_mapping.name
23  end
@lynndylanhurley
Copy link
Owner

What do the request and response headers look like? This can be found in the "network" tab of your web inspector.

@rmagnum2002
Copy link
Author

@lynndylanhurley the response tab was a bunch of html code that looked like usual rails exception errors, so I am not sure this is what you need, however, I'll post the info I have step by step:

  1. Headers when I sign in successfully:

https://gist.github.com/rmagnum2002/8f86bcfb84d3f64ce2cc

  1. Console logs when I sign in successfully:

https://gist.github.com/rmagnum2002/93be6c4ab7ca20f66104

Nothing to show when I navigate through pages, it's all angular and I have the sign out link in the menu, so I assume sign in works as expected.

  1. Headers when I refresh the page (error comes when app does a request to /api/auth#validate_token)
    https://gist.github.com/rmagnum2002/58508565bb7147acffbf
    and
  2. console logs for step 3
    https://gist.github.com/rmagnum2002/013bd50a14ea905565d7

@babelfish
Copy link

I had this same problem when mounting under a namespace (/api/v1 in my case). The problem disappeared after moving the devise mounting out of the namespace.

@aihaddad
Copy link

Hi,
I had the same issue as mentioned, and it actually works if the routes are places outside the namespace. You could keep the namespace effect by mounting it to "/api/auth" instead of just "/auth"

@lauborges
Copy link

+1

2 similar comments
@nicolasiensen
Copy link

+1

@k-zaher
Copy link

k-zaher commented Jul 9, 2015

+1

@goshatch
Copy link

+1. @aihaddad's solution works, but that hardly sounds like the way it should be.

@xanthfan
Copy link

I'm getting this problem as well. Moving the devise mounting out of the namespace, gets me past the "undefined method `name' for nil:NilClass" error. But the validate request is stopped at a different error:

"ArgumentError (wrong number of arguments (2 for 1)):"

Gist with my routes.rb file :
https://gist.github.com/anonymous/c70726923548716fc236

Gist with the console output:
https://gist.github.com/anonymous/0d27838fc166796cd4ca

I've been trying to get ng_token_auth and devise_token_auth to work all week. Love the work you're doing, please help!

@xanthfan
Copy link

Also here are my versions:

devise_token_auth (0.1.32)
  devise (~> 3.3)
  rails (~> 4.2)

@aihaddad
Copy link

I think you need to revise your route to "/auth" instead of "auth"

@xanthfan
Copy link

As it turned out, my bug was not the fault of devise_token_auth. Other git branches of my app (which use the original devise gem) were giving me the same "wrong number of arguments" error. So the problem was with a dependent gem somewhere. Clearing gems and re-bundling solved the problem.

@dkoloditch
Copy link

I experienced the same issue in regard to the /api/v1/ namespace. I moved the mount line from within the namespace in routes...:

namespace :api do
    namespace :v1 do
      mount_devise_token_auth_for 'User', at: 'auth'
      ...
    end
end

...to outside of the namespace while referencing the correct path:

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

namespace :api do
    namespace :v1 do
      ...
    end
end

@ksinghal
Copy link

when I move it out of the namespace as @dkoloditch suggested, the before_filter authenticate_user! conflicts with that of devise. Is there any other way to resolve this issue?

@toshimaru
Copy link

@dkoloditch thaanks for the solution. It works.

@Salet
Copy link

Salet commented Nov 19, 2015

I'm experiencing exact same problems @xanthfan had. When devise_token is mounted under a namespace, I get logged out after page refresh, and when it's outside I hit:

ArgumentError (wrong number of arguments (2 for 1)):
devise (3.5.2) lib/devise.rb:454:in `[]'

However, my Gemfile is almost empty and I continue failing on making this work. Any help appreciated.

@duleorlovic
Copy link

+1 for @dkoloditch solution

@Salet
Copy link

Salet commented Nov 23, 2015

Solution provided by @dkoloditch works. If you however encounter following error:

ArgumentError (wrong number of arguments (2 for 1)):
devise (3.5.2) lib/devise.rb:454:in `[]'

like I and @xanthfan did, clear up your cookies. I had cookies set up by other application using older devise which were causing this.

@andoq
Copy link

andoq commented Mar 10, 2017

The namespaced auth route issue is also discussed in #219

@zachfeldman
Copy link
Contributor

Hi there @rmagnum2002 ,

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