Devise Token Auth installation: default routing settings causing error - devise-token-auth

From a clean create-repack-app install. I add the following to my Gemfile then run bundle:
gem 'devise_token_auth'
Then I run:
rake db:create
rails g devise_token_auth:install
rake db:migrate
Databases (dev and test) are created and ruby files generated (including an addition to the config/routes.rb file). Trying any rake or rails command does the following right now:
rake routes
rake aborted!
NoMethodError: undefined method `devise' for User (call 'User.connection' to establish a connection):Class
Commenting out the following in the config/routes.rb file:
mount_devise_token_auth_for 'User', at: 'auth'
Removes this error. The code added to the User model doesn't contribute to this error. Do I need to run rails g devise:install also? The documentation doesn't mention anything extra. So I'm not sure what I'm doing wrong.

Add below code to the User model
extend Devise::Models
My User model looks like this.
# frozen_string_literal: true
class User < ActiveRecord::Base
extend Devise::Models
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable, :trackable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
include DeviseTokenAuth::Concerns::User
end

I found my answer from another post: Devise_token_auth conflicts?
Adding the following Devise initializer:
config/devise.rb:
Devise.setup do |config|
# The e-mail address that mail will appear to be sent from
# If absent, mail is sent from "please-change-me-at-config-initializers-devise#example.com"
config.mailer_sender = "support#myapp.com"
# ==> ORM configuration
# Load and configure the ORM. Supports :active_record (default) and
# :mongoid (bson_ext recommended) by default. Other ORMs may be
# available as additional gems.
require 'devise/orm/active_record'
# If using rails-api, you may want to tell devise to not use ActionDispatch::Flash
# middleware b/c rails-api does not include it.
# See: https://stackoverflow.com/q/19600905/806956
config.navigational_formats = [:json]
end
Fixed the problem.

Related

Setting session timeout in Rails 6

I need help on the timeout configuration on the rails 6. I tried to configure the /config/initializers/devise.rb, but it's not working, any assist will be appreciated.
Step 1:
Go to /config/initializers/devise.rb configure:
config.timeout_in = 1.minutes
Step 2:
Go to /app/models/user.rb Set :timeoutable in the user model.
class User < ActiveRecord
devise :timeoutable
end
Step 3:
Systemctl restart nginx
Success! 🎉
System informations:
ruby -v
ruby 2.5.7p206 (2019-10-01 revision 67816) [x86_64-linux]
rails -v
Rails 6.0.0
Running on the CentOS 7.
Reference:
https://github.com/plataformatec/devise
Did you set :timeoutable in User model? For example:
class User < ActiveRecord::Base
devise :timeoutable
end
and with this setting set this value config.timeout_in = 1.minutes in /config/initializers/devise.rb as before.
Devise docs:
https://github.com/plataformatec/devise/wiki/How-To:-Add-timeout_in-value-dynamically#this-feature-was-added-in-version-152-and-is-not-available-in-older-versions-of-devise

undefined method 'devise' for User

I have been looking to get to grips with devise and its workings and have kind of fallen at the first hurdle. I have looked in a few places but cannot seem to find someone with this error exactly.
So I have created a simple Home controller with an index view and added root 'home#index' and also ensured the default url options are setup in the development.rb file. I then simply typed:
rails generate devise User
This created my user.rb file in models with the following:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
Pretty straightforward so far, I have the following Gemfile:
source 'https://rubygems.org'
gem 'rails', '4.0.5'
gem 'sqlite3'
gem 'sass-rails', '~> 4.0.2'
gem 'devise'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 1.2'
group :doc do
gem 'sdoc', require: false
end
gem 'bcrypt'
And when I run either rake db:migrate I get the following error:
rake aborted!
NoMethodError: undefined method `devise' for User (call 'User.connection' to establish a connection):Class
/home/jonlee/.rvm/gems/ruby-2.1.1#railstutorial_rails_4_0/gems/activerecord-4.0.5/lib/active_record/dynamic_matchers.rb:22:in `method_missing'
/home/jonlee/Projects/rails/userauth/app/models/user.rb:4:in `<class:User>'
/home/jonlee/Projects/rails/userauth/app/models/user.rb:1:in `<top (required)>'
Im at a loss as to why the User model cannot find the 'devise' method when as far as I can see it is definitely there.
I get similar errors with rake routes, rails server and rails console.
For further info I am using ruby 2.1.1 if that helps?
Add devise to your application Gemfile and install it by running bundle install. After this, you should run the following generator command:
rails generate devise:install
This generator will install an initializer your_application/config/initializers/devise.rb which consists of all the Devise's configuration options.
You missed the above mentioned step which is why the devise configurations are not set and you receive undefined method 'devise' for User error in your model class User.
I ran into a similar issue when I was configuring Devise (Ruby 2.4.1 / Rails 5.1.2). In my case it seems that the following files were not created after I executed: rails generate devise:install for the first time.
create config/initializers/devise.rb
create config/locales/devise.en.yml
Steps that I followed:
1) Comment from your MODEL the following:
#devise :database_authenticatable, :registerable,
#:recoverable, :rememberable, :trackable, :validatable
2) Comment from routes.rb:
#devise_for :sessions
3) Run rails generate devise:install again, you should see that some files are created this time. Hope you it works !
4) Uncomment from 1) & 2)
5) Execute: rake db:migrate
And at this point it should work. Hope it helps someone !
I have the same issue but with other reason. this can also be problem for somebody. Stop rails server
and then type
rails s
to restart it
I've run the generator $ rails generate devise:install but got the same issue.
Anyway it works to me: Add extend Devise::Models to the User models.
In addition to Kirti's answer of running
rails generate devise:install
you may want to rename the devise initializer to
config/initializers/01_devise.rb
because if any other initializer (such as config/initializers/active_admin.rb) runs before devise and touches the ApplicationController, you will get the same error.
And according to
http://guides.rubyonrails.org/configuring.html#using-initializer-files
you control the load order of initializers using file naming.
I had the same issue. The fix was to uncomment:
require 'devise/orm/active_record'
in config/initializers/active_admin.rb.
I deliberately commented it out earlier because documentation says: "Supports :active_record (default)".
Extend Devise::Model
class User < ActiveRecord::Base
extend Devise::Models
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
include DeviseTokenAuth::Concerns::User
end
Reference: https://dev.to/risafj/guide-to-devisetokenauth-simple-authentication-in-rails-api-pfj
So I just had this same error crop up when I did:
rails g controller client/timesheets
It turned out that the problem was with the generated helper:
app/helpers/client/timesheet_helper.rb:
module Client::TimesheetHelper
end
Client is a model that uses Single Table Inheritance off of the User model. Something was getting very mixed up her, what with Client being a Model (class) and a module, and it results in this cryptic error message.
YMMV.
I got the same error when I installed Devise and Mongoid, but forgot to change the orm from active_record to mongoid in config/initializers/devise.rb
Should be:
require 'devise/orm/mongoid'
I had to stop the Spring preloader process:
spring stop
Now works as expected
I had this problem because executed rails g devise user before to rails g devise:install
I solved this, removing line devise_for :users at config/routes.rb
and deleting db/migrate/xxxxxx_add_devise_to_users.rb
now run
rails g devise:install
rails g devise user
I run into this issue as well, solved it by :
Steps:
Comment out all references to devise most importantly in the routes.rb and
user.rb model
run rails g devise:install and be sure you follow the suggested steps
in the guide on screen.
Go on and uncomment references to devise in routes.rb and user.rb
run rake db:migrate
And you good to go!
comment your app/model/user.rb and also comment devise route in config/routes.rb
`rails generate devise:install`
then run
`rails db:migrate`
then uncomment all done before
this method work for me on rails 7.0.0.4
Also ran into the same problem.
For your case, you should first of all stop the server, and restart it. This should work out!
For more insight, you should also consier visiting this site; https://edgeguides.rubyonrails.org/routing.html
I got the same issue now with a lagacy system. The problem was caused by a commented line (remained there at migration from Rails 4 to 5) in the header of config/routes.rb:
#V2p0::Application.routes.draw do
devise_for :users
Rails.application.routes.draw do
devise_for :users
The devise initialization script inserted devise_for also below the commented line. After removing it the problem disappeared.
Comment out:-
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
in user.rb Model. This worked for me

Requiring devise in rspec model spec

I'm using devise in my Admin model like so:
class Admin < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
Functionally, this is working fine. But I want to have specs for my Admin class, so I start to write a spec...
require 'spec_helper'
require 'admin'
describe Admin do
it 'mumblemumble' do ... end
end
...and then when I go to run the spec, it blows up:
active_record/dynamic_matchers.rb:22:in `method_missing': undefined method `devise' for Admin(Table doesn't exist):Class (NoMethodError)
How do I require Devise so that it's available in my spec? It seems like I should be able to
require 'devise'
in either my spec or my model (preferably my model), but this doesn't fix the issue or change my stack trace. How do I require Devise so that my model has the helper method available? I'm poking around the Devise gem but I'm not finding any clues.
How are you running these? RSpec directly? Or bundle exec rake spec?
There's this in your error: for Admin(Table doesn't exist) which makes me wonder if you have a database yet. The rake task should take care of setting up your world for you.
If that doesn't help, post your spec_helper.rb contents too.
Here's a basic Admin model I have:
class Admin < ActiveRecord::Base
devise :database_authenticatable, :recoverable, :rememberable,
:trackable, :validatable
end
And a basic spec:
require 'spec_helper'
describe Admin do
it { should validate_uniqueness_of(:email) }
end
Works great with vanilla generated rails app and generated devise setup.
This error
undefined method `devise' for Admin(Table doesn't exist):Class (NoMethodError)
seems to be that you don't have the table in the db? Did you migrate the rake file?
Yes it seems your test database does not have the admins table.
Try this:
bundle exec rake db:migrate db:test:prepare
db:migrate migrates your development database, if there are any pending migrations and db:test:prepare clones your test database according to the development one.
Got it. I run specs with an in-memory instance of sqlite3, so all the db:test:prepare doesn't apply to me. In addition to requiring Devise, it must also be setup/configured.
So in /spec/support/devise.rb:
require 'devise'
Devise.setup do |config|
require 'devise/orm/active_record'
end
And then in spec_helper.rb:
Dir["./spec/support/**/*.rb"].sort.each {|f| require f}

Rails: User does not respond to 'devise' method” when running “rails generate devise:install"

I have been working on this issue for hours now and haven't seemed to find anything exactly like this on StackOverflow.
C:\Sites\isawyou>rake db:migrate
rake aborted!
User does not respond to 'devise' method. This usually means you haven't loaded
your ORM file or it's being loaded too late. To fix it, be sure to require 'devi
se/orm/YOUR_ORM' inside 'config/initializers/devise.rb' or before your applicati
on definition in 'config/application.rb'
C:/Sites/isawyou/config/routes.rb:2:in `block in <top (required)>'
C:/Sites/isawyou/config/routes.rb:1:in `<top (required)>'
C:2:in `rescue in execute_if_updated'
C:in `execute_if_updated'
C:/Sites/isawyou/config/environment.rb:5:in `<top (required)>'
Tasks: TOP => db:migrate => environment
(See full trace by running task with --trace)
Issue: Everytime I try to migrate I get this error. However, I do not have a devise.rb file installed so am not sure how to fix this..
Attempts to solve: tried this answer however that didn't let me populate the correct rake routes I needed (ie: new user sessions, create new user, etc..)
Tried redoing the order of commands (as shown below).. But that didn't work either.. I always get stuck at the "rake db:migrate" portion..
$ rails d devise User
$ rails generate devise:install (you may have to override previous files)
$ rails generate devise User
$ rake db:drop
$ rake db:create
$ rake db:migrate
$ rake routes
Thank you in advance if you know the fix!!
It looks like you have devise generator previously run unsuccessfully. Backtrace gives a clue that error happens within config/routes.rb line 2. I guess there is a following line of code, setting up devise routes
devise_for :users
But your users model doesn't have devise modules setup. There should be something like
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :timeoutable and :omniauthable
devise :database_authenticatable, :rememberable, :trackable, :validatable
If this line is present it means that devise is improperly installed.
Also there is a chance that you have another class or module "User" within ActiveSupport::Dependencies autoloader paths and when devise_for :users line requires User class this class/module loaded first, whereas your real User module is not loaded. The "invalid" user module/class is not ActiveRecord descendant, so devise AR hook is not installed to it, so it doesn't have #devise method. So make sure you don't have any other classes/modules named User

Rails3/Devise: undefined method new_application_user_session_path

Following the README documentation, I installed Devise into an existing Rails 3 application. However, it doesn't seem to work. When I try to access one of my controllers that have before_filter :authenticate_application_user!, I get the following error message:
undefined method 'new_application_user_session_path' for #<Devise::FailureApp::0x60aldc0>
I have no idea why. I've checked numerous to see if I followed the installation guide correctly, to no avail. So I'm wondering if anyone cold help me out.
Here is my routes.rb
AwesomeApp::Application.routes.draw do
devise_for :application_users
root :to => "home#index"
scope "admin" do
resources :application_users, :path => "users"
end
end
Here is my Gemfile:
source 'http://rubygems.org'
gem 'rails', '3.0.0'
gem 'ruby-oci8'
gem 'activerecord-oracle_enhanced-adapter'
gem 'warbler'
gem 'devise', '1.1.3'
group :development, :test do
gem 'factory_girl_rails'
gem 'forgery'
end
Here is my migration file for updating my existing database schema:
class DeviseCreateApplicationUsers < ActiveRecord::Migration
def self.up
change_table(:application_users) do |t|
t.rememberable
t.trackable
end
end
def self.down
change_table(:application_users) do |t|
t.remove :rememberable, :trackable
end
end
end
NOTE: In case anyone is wondering, I will be authenticating using LDAP. So that's why you don't see t.database_authenticatable
Here is my model application_user.rb:
class ApplicationUser < AbstractModel
devise :rememberable, :trackable, :timeoutable
end
Here is my devise.rb initializer file:
Devise.setup do |config|
config.mailer_sender = "please-change-me#config-initializers-devise.com"
require 'devise/orm/active_record'
config.authentication_keys = [ :user_ldap_id ]
config.stretches = 10
config.encryptor = :bcrypt
config.pepper = "SALT AND PEPPERS HERE, SALT SALT SALT"
end
NOTE: Because I will be authenticating against LDAP, I am using a different column other than the default email column that Devise defaults to. As a result, I had to install the Devise views (rails g devise:views) and modify the app/views/devise/session/new.html.erb file to use user_ldap_id instead of email.
NOTE: I removed any irrelevant code from these snippets.
The problem was because I did not have the database_authenticable enabled in my model. Without that, no routes will be generated. But because I don't need it, there was no point. However, to resolve this issue, I had to install Devise LDAP Authenticatable and enabled it in my model. Once I did that and restarted the server, the route became accessible.
Take a look at this ticket I created on the Devise Github repo. Someone there was able to walk me through the entire thing. Quite impressed actually at how responsive they were.
https://github.com/plataformatec/devise/issues/issue/614/#comment_517418
Added the following to my Gemfile:
gem 'devise_ldap_authenticatable'
Added the following to my application_users.rb:
devise :ldap_authenticatable

Resources