[RUBY] [Rails] Solving the problem that session timeout does not work

Premise

-Define a timeout method -Even though the code itself is not wrong, the session is judged as nil, the method is not processed well, and an error occurs.

Timeout method

sessions_controller.rb



  def create
# ...

   session[:last_access_time] = Time.current

# ...
  end

application_controller.rb


  TIMEOUT = 5.minutes

  def time_out
     if session[:last_access_time] > TIMEOUT.ago
       session[:last_access_time] = Time.current
     else
       session.delete(:user_id)
       flash[:danger] = "Timed out."
       redirect_to :login
      end
    end

When I build the above method and then try to log in, an error occurs.

NoMethodError (undefined method `>=' for nil:NilClass):
# session[:last_access_time]Is judged to be nil.

Solution

I solved it by deleting the cookie on the browser side. Probably because I defined the method while logged in You may have been angry at "there is no session [: last_access_time]".

Recommended Posts

[Rails] Solving the problem that session timeout does not work
[Rails] The problem that pry-byebug does not stop through breakpoints
Does the escape sequence (\) not work? (for Mac)
Sidekiq-limit_fetch does not work
When the server does not start with rails s
[Railways] Railways does not work when the RubyMine project root is other than the Rails root directory.
[Ruby on Rails] When parameter id acquisition does not work
About the matter that: nth-child (-n + m) does not work as intended in the selector of jsoup
How to solve the problem that the website image is not displayed after deploying to heroku on Rails 5
[Firebase] The problem that the currentUser of Firebase Auth does not become null even if the app is uninstalled.
Solved the problem that the scroll event did not fire in JQuery
[Rails] Session timeout setting in devise
How to interact with a server that does not crash the app
[Rails] About the error that the image is not displayed in the production environment
When the form such as Select of Materialize does not work properly
[Rails] Video does not play with video_tag
Java Integer comparison (==) does not work correctly
eclipse tab width change does not work.
JavaScript (vanilla) does not respond in Rails.
Solving the knapsack problem with dynamic programming
How to make @Transactional work that does not work if you use it incorrectly
What to do when rails db: seed does not reflect in the database
Setting method that the size does not change even if CSS is changed
If the ctrl + shortcut does not work in Eclipse, disabling welcome may help
[Rails] About the problem that the video thumbnail automatically created by FFmpeg is not displayed [AWS S3 + EC2 + CarrierWave + Fog]
The problem that the JDK set in JAVA_HOME does not appear even if java -version is performed at the command prompt