Setting session timeout in Rails 6 - ruby-on-rails

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

Related

Devise Token Auth installation: default routing settings causing error

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.

RoR 3.2 devise (3.2.2) :timeoutable not working

After checking a few questions like:
rails 3.1, devise gem, :timeoutable is not working, why?
Devise - Timeout not working
devise config.timeout_in not working
How to configure time out using devise?
I have set my user model with :timeoutable
class User < ActiveRecord::Base
devise :rememberable, :demo_authenticatable, :api_authenticatable,:registerable,
:recoverable, :trackable, :validatable, :confirmable, :lockable, :timeoutable
And I have also uncomented the timeout_in line in the devise.rb file
config.timeout_in = 1.minutes
I also tryed setting the timeout_in property in the user model, but i keep beeing logged in
Any clues?
Thanks in advance, cheers!
If you're expecting it to present you with the login page when you refresh, remove the timeout part from your model and put it in devise.rb NOT development.rb & restart rails server.
config.timeout_in = 1.hour

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

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

Heroku [WARNING] You provided devise_for :users but there is no model User defined in your application

I have an rails 3 (3.0.9) application working on ruby 1.8.7 with the gem devise (1.4.2) on my computer working perfectly.
I tried to push it on heroku and i got the following error message on application load:
[WARNING] You provided devise_for :users but there is no model User defined in your application
=> Booting WEBrick
=> Rails 3.0.9 application starting in production on http://0.0.0.0:43292
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.0.9/lib/active_support/inflector/methods.rb:124:in `block in constantize': uninitialized constant User (NameError)
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.0.9/lib/active_support/inflector/methods.rb:123:in `each'
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.0.9/lib/active_support/inflector/methods.rb:123:in `constantize'
from /app/vendor/bundle/ruby/1.9.1/gems/devise-1.4.2/lib/devise/mapping.rb:84:in `to'
from /app/vendor/bundle/ruby/1.9.1/gems/devise-1.4.2/lib/devise/mapping.rb:79:in `modules'
from /app/vendor/bundle/ruby/1.9.1/gems/devise-1.4.2/lib/devise/mapping.rb:88:in `strategies'
from /app/vendor/bundle/ruby/1.9.1/gems/devise-1.4.2/lib/devise.rb:410:in `block in configure_warden!'
from /app/vendor/bundle/ruby/1.9.1/gems/devise-1.4.2/lib/devise.rb:409:in `each_value'
from /app/vendor/bundle/ruby/1.9.1/gems/devise-1.4.2/lib/devise.rb:409:in `configure_warden!'
The problem comes from devise but i don't know how to fix it.
My model User is correctly defined, and it works on my computer...
Does anyone know how to fix this ?
Thanks for you help
Make sure you have this:
class User < ActiveRecord::Base
Defined at the top of your user/devise model.
ALso, make sure you run your migrations.
heroku rake db:migrate
In console.
Check inside your users.rb to make sure that you spelled ':database_authenticable' correctly, this seems to be a common problem with this error.
class User < ActiveRecord::Base
devise :database_authenticable, :recoverable,
:rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
end
Also, I suggest you take a look here, to see how these people fixed the error.
http://groups.google.com/group/plataformatec-devise/browse_thread/thread/807f4c6e3475622f

Resources