Skip to content

Commit

Permalink
Merge pull request #4830 from rubyforgood/get-login-duration-from-bac…
Browse files Browse the repository at this point in the history
…kend

Get login duration from backend
  • Loading branch information
FireLemons committed Aug 9, 2023
2 parents 844ca55 + b3a81b6 commit b46a5d0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
7 changes: 7 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions app/javascript/src/session_timeout_poller.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
<%= og_tag :image, content: image_url('login.jpg') %>
<%= render 'shared/favicons' %>
<script>
window.timeout = <%= @timeout_duration.to_json %>
</script>

<%= javascript_include_tag "application", "data-turbo-track": "reload", defer: true %>
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
Expand Down
15 changes: 15 additions & 0 deletions spec/system/application/timeout_warning_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b46a5d0

Please sign in to comment.