title: 2 minutes before session timeout, warn the user and extend it with Devise | Rails 5
body: <p>To auto logout user for being <code>idle/inactive</code> till 20 minutes. I have used Devise module <strong><a href="https://www.rubydoc.info/github/plataformatec/devise/Devise/Models/Timeoutable" rel="nofollow noreferrer"><code>:timeoutable</code></a></strong> and also set <code>config.timeout_in = 20.minutes</code> in devise configuration file. Which works perfectly fine.</p>
  
  <p>Now when user have been inactive in the application for 18 minutes, would like to notify him </p>
  
  <blockquote>
    <p>"You will be sign-out after 2 minutes due to your inactivity. If you
    wish to continue your session press Ok"</p>
  </blockquote>
  
  <p>To achieve this I have added <code>setInterval()</code> function inside <strong>application.js</strong> which will make Ajax call each minute to check user's session (I have set expire time manually in session as below). </p>
  
  <p><strong>application_controller.rb:</strong></p>
  
  <pre><code>class ApplicationController &lt; ActionController::Base
    before_action :renew_user_session
  
    def renew_user_session
      session[:expires_at] = (Time.now + 20.minutes).to_s
    end
  end
  </code></pre>
  
  <p><strong>users_controller.rb:</strong></p>
  
  <pre><code>class UsersController &lt; ApplicationController
    skip_before_action :renew_user_session, only: :validate_user_session
  
    # this method is called in setInterval() through ajax
    def validate_user_session
      status = true
      if user_signed_in?
        status = (Time.parse(session[:expires_at]) - 2.minutes) &gt; Time.now
      end
      respond_to do |format|
        format.json { render json: status }
      end
    end
  end
  </code></pre>
  
  <p>but whenever I make Ajax call, it activate user. Is there anyway to check user's inactivity time? and notify before 2 minutes? or any alternate solution to achieve this?</p>