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
Getting a NameError - undefined method `authenticate_user!'? (R: Check the token authenticatable model name.) #42
Comments
Hello @JeskTop! Did you install Devise before installing Simple Token Authentication? (It's a requirement because Simple Token Authentication relies on it to authenticate users.) |
Hi. @gonzalo-bulnes |
@JeskTop when I had this issue, it was because I did not type the correct resource name after The other time this happened was while writing specifications in rspec. As a matter of fact, I just replicated this issue when running rspec. It was not fixed by including the Are you getting this with rspec by chance? |
@JeskTop Both gems versions are compatibe with the use of Simple Token Authentication. The Oh, did you install Devise for the |
Hi @nicolo! First of all: could you please re-post your question here? The cause of it , hence the response, may be different, but the issue you're facing is the same. In order to keep the questions easy to browse, I would prefer to keep together all aspects of each strange/confusing/unexpected behaviour, and only separate topics when they clearly diverge. |
I've read this #67
Once I do this, I'm able to access any controller without any token provided. It's basically like authentication doesn't exist. If I don't include fallback_to_devise: false, I get undefined method `authenticate_user!' on every request. I'm trying to create an app which is purely an API like explained in issue 67. Feels like I'm missing something major. Thanks in advance for any help. Here are some relevant files that should help with troubleshooting
|
@nicolo Now answering your question: 1. The rails generate devise User Before installing Simple Token Authentication, as a first step, you must make sure that you can authenticate with the default # app/models/user.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
# Enable authentication with Devise
before_action :authenticate_user!
end The undefined method: `authenticate_user!' message indicates without doubt that the error, forgotten instruction or misconfiguration occurred within this first step. I know you are sure of having properly set Devise up, but believe me, there is no doubt about that, please give it another chance. 2. Once that solved, I have a few comments about the code you posted:
class ApplicationController < ActionController::Base
# Enable token authentication in a context where there is no CSRF protection
# See https://github.com/gonzalo-bulnes/simple_token_authentication/issues/49
acts_as_token_authentication_handler_for User, fallback_to_devise: false
end
class ApplicationController < ActionController::Base
# Enable CSRF protection for API
# See https://github.com/gonzalo-bulnes/simple_token_authentication/issues/67 and
# http://edgeapi.rubyonrails.org/classes/ActionController/RequestForgeryProtection.html
protect_from_forgery with: :null_session
# Enable token authentication in this specific CSRF protection context
acts_as_token_authentication_handler_for User
end That was a bit long but I hope it helps! EDIT: Thanks for re-posting your comment, it makes the conversation easier to follow! |
@gonzalo-bulnes thanks for your help. I appreciate all your work on this gem. I think I discovered my problem. I did setup Devise correctly, but I then broke things later by tinkering with my routes file. I had moved devise_for users inside my api namespace. It seems like I need to include it within the namespace and outside of it. *note I added in all the skips for the global devise_for users because I only want to expose those routes within the api
|
Thanks for your feedback @nicolo, I'll close this issue. Don't hesitate to re-open it if you run into the problem again; even if they're not exactly Simple Token Authentication issues, that's always good to keep a trace of the different kind of issues that can occur when using the gem. Regards! |
Thanks @nicolo. It works for me too. I put devise_for outside namespace api and worked. |
I added this line to my controller which did the trick for me:
|
Thank you for sharing @amitfriedman12! |
None of the above works for me. |
Why I set
acts_as_token_authentication_handler_for User
to my ApplicationController, I will have the problem isNameError - undefined method
authenticate_user!'`. I am the reference document, but also has this problem, I don't know what is happen.The text was updated successfully, but these errors were encountered: