[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
[Firebase] The problem that the currentUser of Firebase Auth does not become null even if the app is uninstalled.
Solving the knapsack problem with dynamic programming
If the ctrl + shortcut does not work in Eclipse, disabling welcome may help