diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index ea3142cdb1..c364838016 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -7,6 +7,7 @@ class ApplicationController < ActionController::Base before_action :store_user_location!, if: :storable_location? before_action :authenticate_user! before_action :set_current_user + before_action :set_timeout_duration before_action :set_current_organization after_action :verify_authorized, except: :index, unless: :devise_controller? # after_action :verify_policy_scoped, only: :index @@ -101,6 +102,12 @@ def set_current_user RequestStore.store[:current_user] = current_user end + def set_timeout_duration + return unless current_user + + @timeout_duration = current_user.timeout_in + end + def set_current_organization RequestStore.store[:current_organization] = current_organization end diff --git a/app/javascript/src/session_timeout_poller.js b/app/javascript/src/session_timeout_poller.js index 155b27819d..a42478801a 100644 --- a/app/javascript/src/session_timeout_poller.js +++ b/app/javascript/src/session_timeout_poller.js @@ -1,7 +1,7 @@ -const deviseTimeoutInMinutes = 180 -const twoMinuteWarning = deviseTimeoutInMinutes - 2 +const timeout = window.timeout || 180 +const twoMinuteWarning = timeout - 2 const totalTimerAmount = twoMinuteWarning * 60 * 1000 -const deviseTimeoutInMilliseconds = deviseTimeoutInMinutes * 60 * 1000 +const deviseTimeoutInMilliseconds = timeout * 60 * 1000 const startTime = new Date().getTime() let lastTime = new Date().getTime() let currentTime @@ -17,7 +17,6 @@ setInterval(myTimer, 1000) function myTimer () { timeElapsed = Math.abs(lastTime - startTime) currentTime = new Date().getTime() - // console.log('Should go up by 1 second', timeElapsed) if (timeElapsed > deviseTimeoutInMilliseconds) { window.location.reload() } else if (timeElapsed > totalTimerAmount) { diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 55a69148a5..a8fe1fbebf 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -15,6 +15,9 @@ <%= og_tag :image, content: image_url('login.jpg') %> <%= render 'shared/favicons' %> + <%= javascript_include_tag "application", "data-turbo-track": "reload", defer: true %> <%= stylesheet_link_tag "application", "data-turbo-track": "reload" %> diff --git a/spec/system/application/timeout_warning_spec.rb b/spec/system/application/timeout_warning_spec.rb new file mode 100644 index 0000000000..bc6db8d4ee --- /dev/null +++ b/spec/system/application/timeout_warning_spec.rb @@ -0,0 +1,15 @@ +require "rails_helper" + +RSpec.describe "/*", type: :system do + context "when user is signed in" do + let(:user) { create(:volunteer) } + before do + sign_in user + end + it "renders the seconds before logout as a javascript variable" do + visit "/" + parsed_page = Nokogiri::HTML(page.html) + expect(parsed_page.at("script").text.strip).to include(user.timeout_in.in_seconds.to_s) + end + end +end