ucb_rails_session_timeout 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/CHANGELOG.md +3 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +134 -0
- data/Rakefile +8 -0
- data/app/javascript/ucb_rails_session_timeout/activity_timer.js +75 -0
- data/lib/ucb_rails_session_timeout/activity_tracker.rb +34 -0
- data/lib/ucb_rails_session_timeout/config.rb +41 -0
- data/lib/ucb_rails_session_timeout/version.rb +5 -0
- data/lib/ucb_rails_session_timeout/view_helper.rb +18 -0
- data/lib/ucb_rails_session_timeout.rb +15 -0
- data/sig/ucb_rails_session_timeout.rbs +4 -0
- data/vendor/assets/javascripts/ucb_rails_session_timeout.js +75 -0
- metadata +89 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: eec79400b51768c1573bed8ff84536507dbc9e1b4f46bde74f14c01b20888498
|
4
|
+
data.tar.gz: 5e6f7db2019778890f0b9e21684adf69d357160bec91d8fa68b271888e6daaf7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8751d53d5f07d428f571f534b52e886a26af511346f68240f6e4947ae4e9f3dee1ea4c9ffe48e2fa5dc21a25ed89652108660da757f0c5d6c10c49e998ddedd1
|
7
|
+
data.tar.gz: 216d18fd7232bbe2f06a1149d2f6ac2679f61f94852822526c917bd225bb23fbc3d436e35eba75acd93ac4cd97a53f91dfbc09573744e49e8df62afb28eaf227
|
data/.rspec
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
10
|
+
identity and orientation.
|
11
|
+
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
13
|
+
diverse, inclusive, and healthy community.
|
14
|
+
|
15
|
+
## Our Standards
|
16
|
+
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
18
|
+
community include:
|
19
|
+
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
24
|
+
and learning from the experience
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
26
|
+
community
|
27
|
+
|
28
|
+
Examples of unacceptable behavior include:
|
29
|
+
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
31
|
+
any kind
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
33
|
+
* Public or private harassment
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
35
|
+
without their explicit permission
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
37
|
+
professional setting
|
38
|
+
|
39
|
+
## Enforcement Responsibilities
|
40
|
+
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
44
|
+
or harmful.
|
45
|
+
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
49
|
+
decisions when appropriate.
|
50
|
+
|
51
|
+
## Scope
|
52
|
+
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
54
|
+
an individual is officially representing the community in public spaces.
|
55
|
+
Examples of representing our community include using an official email address,
|
56
|
+
posting via an official social media account, or acting as an appointed
|
57
|
+
representative at an online or offline event.
|
58
|
+
|
59
|
+
## Enforcement
|
60
|
+
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
62
|
+
reported to the community leaders responsible for enforcement at
|
63
|
+
[INSERT CONTACT METHOD].
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
65
|
+
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
67
|
+
reporter of any incident.
|
68
|
+
|
69
|
+
## Enforcement Guidelines
|
70
|
+
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
73
|
+
|
74
|
+
### 1. Correction
|
75
|
+
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
77
|
+
unprofessional or unwelcome in the community.
|
78
|
+
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
82
|
+
|
83
|
+
### 2. Warning
|
84
|
+
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
86
|
+
actions.
|
87
|
+
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
93
|
+
ban.
|
94
|
+
|
95
|
+
### 3. Temporary Ban
|
96
|
+
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
98
|
+
sustained inappropriate behavior.
|
99
|
+
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
101
|
+
communication with the community for a specified period of time. No public or
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
104
|
+
Violating these terms may lead to a permanent ban.
|
105
|
+
|
106
|
+
### 4. Permanent Ban
|
107
|
+
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
111
|
+
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
113
|
+
community.
|
114
|
+
|
115
|
+
## Attribution
|
116
|
+
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
118
|
+
version 2.1, available at
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
120
|
+
|
121
|
+
Community Impact Guidelines were inspired by
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
123
|
+
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
127
|
+
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Darin Wilson
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
# UCB Rails Session Timeout
|
2
|
+
|
3
|
+
Rails gem that provides configurable session timeouts and warning notifications. When users are inactive, they receive a warning dialog and can choose to extend their session or be logged out.
|
4
|
+
|
5
|
+
## Features
|
6
|
+
- Configurable timeout and warning periods
|
7
|
+
- Native HTML dialog warning
|
8
|
+
- Automatic session tracking
|
9
|
+
- No external dependencies
|
10
|
+
- Supports Rails 7+
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem "ucb_rails_session_timeout"
|
16
|
+
```
|
17
|
+
|
18
|
+
## Ruby Setup
|
19
|
+
|
20
|
+
Add to your `ApplicationController`:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
class ApplicationController < ActionController::Base
|
24
|
+
include UcbRailsSessionTimeout
|
25
|
+
|
26
|
+
...
|
27
|
+
end
|
28
|
+
```
|
29
|
+
|
30
|
+
By default, the warning popup will appear after 13 minutes, the session will timeout after 15 minutes, and the current page will reload (which should fail, as the user should already be logged out). You can customize any/all of these behaviors by creating an initializer and changing the values:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
# config/initializers/ucb_rails_session_timeout.rb
|
34
|
+
|
35
|
+
UcbRailsSessionTimeout.configure do |config|
|
36
|
+
# timeout length as a duration - default is 15.minutes
|
37
|
+
config.timeout_length = 20.minutes
|
38
|
+
|
39
|
+
# warning length as a duration: the warning will popup this length of time before the timeout - default is 2.minutes
|
40
|
+
config.warning_length = 5.minutes
|
41
|
+
|
42
|
+
# the URL to redirect to when the session times out - default is the current URL
|
43
|
+
# for more reliable behavior, set this to a path that will actually log the user out
|
44
|
+
config.redirect_url = "/logout"
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
If you want to do something besides redirecting the user after the session times out, you can override the `handle_session_timeout` method in the `ApplicationController` and implement whatever behavior you'd like:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
class ApplicationController < ActionController::Base
|
52
|
+
...
|
53
|
+
|
54
|
+
def handle_session_timeout
|
55
|
+
logger.info "Session timed out"
|
56
|
+
redirect_to "/login", alert: "You have been logged out"
|
57
|
+
end
|
58
|
+
|
59
|
+
...
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
## JavaScript Setup (optional)
|
64
|
+
|
65
|
+
If you don't want the popup warning, you can skip this step.
|
66
|
+
|
67
|
+
### Propshaft setup
|
68
|
+
|
69
|
+
Import the timer so it's available to the view.
|
70
|
+
|
71
|
+
```javascript
|
72
|
+
// app/javascript/application.js
|
73
|
+
import { ActivityTimer } from "@ucb_rails_session_timeout/activity_timer"
|
74
|
+
```
|
75
|
+
|
76
|
+
|
77
|
+
### Sprockets setup
|
78
|
+
|
79
|
+
Add this to your `application.js`
|
80
|
+
|
81
|
+
```javascript
|
82
|
+
//= require ucb_rails_session_timeout
|
83
|
+
```
|
84
|
+
|
85
|
+
### Add the popup script to your view
|
86
|
+
|
87
|
+
The `ucb_rails_session_timeout_script` helper will insert a JavaScript snippet that initializes the timer for the popup. Call the helper from anywhere in your view layer (e.g. your application layout file):
|
88
|
+
|
89
|
+
```haml
|
90
|
+
= ucb_rails_session_timeout_script()
|
91
|
+
```
|
92
|
+
|
93
|
+
Most likely, you'll only want the popup warning on pages where the user is actually logged in:
|
94
|
+
|
95
|
+
```haml
|
96
|
+
- if current_user.present?
|
97
|
+
= ucb_rails_session_timeout_script()
|
98
|
+
```
|
99
|
+
|
100
|
+
|
101
|
+
## Styling
|
102
|
+
|
103
|
+
The warning dialog uses the native HTML `<dialog>` element with a CSS class `.ucb-rails-session-timeout-dialog`. You can use the class in your CSS files as a hook to style the dialog however you'd like:
|
104
|
+
|
105
|
+
```css
|
106
|
+
.ucb-rails-session-timeout-dialog {
|
107
|
+
/* override dialog styles */
|
108
|
+
}
|
109
|
+
```
|
110
|
+
|
111
|
+
If you're using Bootstrap with SASS, you can use the `@extend` feature to integrate Bootstrap's modal styles:
|
112
|
+
|
113
|
+
```scss
|
114
|
+
.ucb-rails-session-timeout-dialog {
|
115
|
+
@extend .modal;
|
116
|
+
|
117
|
+
header {
|
118
|
+
@extend .modal-header;
|
119
|
+
}
|
120
|
+
|
121
|
+
main {
|
122
|
+
@extend .modal-body;
|
123
|
+
}
|
124
|
+
|
125
|
+
footer {
|
126
|
+
@extend .modal-footer;
|
127
|
+
|
128
|
+
button {
|
129
|
+
@extend .btn;
|
130
|
+
@extend .btn-primary;
|
131
|
+
}
|
132
|
+
}
|
133
|
+
}
|
134
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
export class ActivityTimer {
|
2
|
+
constructor(timeoutLengthInSeconds, warningLengthInSeconds, redirectUrl) {
|
3
|
+
this.timeoutLengthInSeconds = timeoutLengthInSeconds
|
4
|
+
this.warningLengthInSeconds = warningLengthInSeconds
|
5
|
+
this.redirectUrl = redirectUrl
|
6
|
+
this.dialog = this.setupDialog()
|
7
|
+
this.setupTimer()
|
8
|
+
}
|
9
|
+
|
10
|
+
setupDialog() {
|
11
|
+
const dialog = document.createElement("dialog")
|
12
|
+
dialog.className = "ucb-rails-session-timeout-dialog"
|
13
|
+
dialog.innerHTML = `
|
14
|
+
<header>
|
15
|
+
<h3>Session Timeout Warning</h3>
|
16
|
+
</header>
|
17
|
+
<main>
|
18
|
+
Your session will expire in <span id="session_timeout_countdown"></span>
|
19
|
+
</main>
|
20
|
+
<footer>
|
21
|
+
<button>Stay Logged In</button>
|
22
|
+
</footer>
|
23
|
+
`
|
24
|
+
document.body.appendChild(dialog)
|
25
|
+
dialog.querySelector("button").addEventListener("click", () => {
|
26
|
+
this.extendSession()
|
27
|
+
dialog.close()
|
28
|
+
})
|
29
|
+
return dialog
|
30
|
+
}
|
31
|
+
|
32
|
+
setupTimer() {
|
33
|
+
this.timer = setTimeout(() => {
|
34
|
+
if (!this.dialog.open) {
|
35
|
+
this.showWarning()
|
36
|
+
}
|
37
|
+
}, (this.timeoutLengthInSeconds - this.warningLengthInSeconds) * 1000)
|
38
|
+
}
|
39
|
+
|
40
|
+
showWarning() {
|
41
|
+
document.querySelector("dialog.ucb-rails-session-timeout-dialog").showModal()
|
42
|
+
this.startCountdown()
|
43
|
+
}
|
44
|
+
|
45
|
+
startCountdown() {
|
46
|
+
const countdownEl = document.getElementById("session_timeout_countdown")
|
47
|
+
const timeoutAt = Date.now() + (this.warningLengthInSeconds * 1000)
|
48
|
+
|
49
|
+
const updateCountdown = () => {
|
50
|
+
const remainingSeconds = Math.max(0, Math.round((timeoutAt - Date.now()) / 1000))
|
51
|
+
const minutes = Math.floor(remainingSeconds / 60)
|
52
|
+
const seconds = remainingSeconds % 60
|
53
|
+
|
54
|
+
countdownEl.textContent = `${minutes}:${seconds.toString().padStart(2, "0")}`
|
55
|
+
|
56
|
+
if (remainingSeconds <= 0) {
|
57
|
+
clearInterval(this.countdownTimer)
|
58
|
+
if (this.redirectUrl) {
|
59
|
+
window.location.href = this.redirectUrl
|
60
|
+
} else {
|
61
|
+
window.location.reload()
|
62
|
+
}
|
63
|
+
}
|
64
|
+
}
|
65
|
+
|
66
|
+
updateCountdown() // Initial update
|
67
|
+
this.countdownTimer = setInterval(updateCountdown, 1000)
|
68
|
+
}
|
69
|
+
|
70
|
+
extendSession() {
|
71
|
+
fetch(window.location.href).catch(() => {})
|
72
|
+
clearInterval(this.countdownTimer)
|
73
|
+
this.setupTimer()
|
74
|
+
}
|
75
|
+
}
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require "active_support/core_ext/numeric/time"
|
2
|
+
|
3
|
+
module UcbRailsSessionTimeout
|
4
|
+
module ActivityTracker
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
# build in a little wiggle room to account for timing differences between front-end and back-end
|
8
|
+
GRACE_PERIOD = 15.seconds
|
9
|
+
|
10
|
+
included do
|
11
|
+
before_action :check_timeout
|
12
|
+
after_action :track_activity
|
13
|
+
end
|
14
|
+
|
15
|
+
def handle_session_timeout
|
16
|
+
redirect_to UcbRailsSessionTimeout.configuration.redirect_url || request.original_url
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def track_activity
|
22
|
+
session[:last_active_at] = Time.current.strftime("%d/%m/%Y, %H:%M:%S")
|
23
|
+
end
|
24
|
+
|
25
|
+
def check_timeout
|
26
|
+
return if session[:last_active_at].blank?
|
27
|
+
time_since_last_activity = Time.current - Time.parse(session[:last_active_at])
|
28
|
+
if time_since_last_activity > (UcbRailsSessionTimeout.configuration.timeout_length + GRACE_PERIOD)
|
29
|
+
reset_session
|
30
|
+
handle_session_timeout()
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require "active_support/core_ext/numeric/time"
|
2
|
+
|
3
|
+
module UcbRailsSessionTimeout
|
4
|
+
class << self
|
5
|
+
def configuration
|
6
|
+
@configuration ||= UcbRailsSessionTimeout::Config.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def configure
|
10
|
+
yield configuration
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Config
|
15
|
+
attr_accessor :timeout_length, :warning_length, :redirect_url
|
16
|
+
|
17
|
+
def initialize
|
18
|
+
@timeout_length = 15.minutes
|
19
|
+
@warning_length = 2.minutes
|
20
|
+
@redirect_url = nil
|
21
|
+
end
|
22
|
+
|
23
|
+
def timeout_length=(value)
|
24
|
+
validate_duration!(:timeout_length, value)
|
25
|
+
@timeout_length = value
|
26
|
+
end
|
27
|
+
|
28
|
+
def warning_length=(value)
|
29
|
+
validate_duration!(:warning_length, value)
|
30
|
+
@warning_length = value
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def validate_duration!(field, value)
|
36
|
+
unless value.is_a?(ActiveSupport::Duration)
|
37
|
+
raise UcbRailsSessionTimeout::Error.new("#{field} must be an instance of ActiveSupport::Duration (e.g. 15.minutes, 60.seconds, etc)")
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "action_view"
|
2
|
+
|
3
|
+
module UcbRailsSessionTimeout
|
4
|
+
module ViewHelper
|
5
|
+
def ucb_rails_session_timeout_script
|
6
|
+
js = <<-JS
|
7
|
+
document.addEventListener("DOMContentLoaded", () => { new ActivityTimer(
|
8
|
+
#{UcbRailsSessionTimeout.configuration.timeout_length.to_i},
|
9
|
+
#{UcbRailsSessionTimeout.configuration.warning_length.to_i},
|
10
|
+
#{"'" + UcbRailsSessionTimeout.configuration.redirect_url + "'" || "null"}
|
11
|
+
) })
|
12
|
+
JS
|
13
|
+
content_tag(:script, js.html_safe)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
ActionView::Base.send :include, UcbRailsSessionTimeout::ViewHelper
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "active_support"
|
2
|
+
require "ucb_rails_session_timeout/activity_tracker"
|
3
|
+
require "ucb_rails_session_timeout/config"
|
4
|
+
require "ucb_rails_session_timeout/view_helper"
|
5
|
+
|
6
|
+
module UcbRailsSessionTimeout
|
7
|
+
class Error < StandardError; end
|
8
|
+
|
9
|
+
class Engine < ::Rails::Engine
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.included(base)
|
13
|
+
base.include ActivityTracker
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
class ActivityTimer {
|
2
|
+
constructor(timeoutLengthInSeconds, warningLengthInSeconds, redirectUrl) {
|
3
|
+
this.timeoutLengthInSeconds = timeoutLengthInSeconds
|
4
|
+
this.warningLengthInSeconds = warningLengthInSeconds
|
5
|
+
this.redirectUrl = redirectUrl
|
6
|
+
this.dialog = this.setupDialog()
|
7
|
+
this.setupTimer()
|
8
|
+
}
|
9
|
+
|
10
|
+
setupDialog() {
|
11
|
+
const dialog = document.createElement("dialog")
|
12
|
+
dialog.className = "ucb-rails-session-timeout-dialog"
|
13
|
+
dialog.innerHTML = `
|
14
|
+
<header>
|
15
|
+
<h3>Session Timeout Warning</h3>
|
16
|
+
</header>
|
17
|
+
<main>
|
18
|
+
Your session will expire in <span id="session_timeout_countdown"></span>
|
19
|
+
</main>
|
20
|
+
<footer>
|
21
|
+
<button>Stay Logged In</button>
|
22
|
+
</footer>
|
23
|
+
`
|
24
|
+
document.body.appendChild(dialog)
|
25
|
+
dialog.querySelector("button").addEventListener("click", () => {
|
26
|
+
this.extendSession()
|
27
|
+
dialog.close()
|
28
|
+
})
|
29
|
+
return dialog
|
30
|
+
}
|
31
|
+
|
32
|
+
setupTimer() {
|
33
|
+
this.timer = setTimeout(() => {
|
34
|
+
if (!this.dialog.open) {
|
35
|
+
this.showWarning()
|
36
|
+
}
|
37
|
+
}, (this.timeoutLengthInSeconds - this.warningLengthInSeconds) * 1000)
|
38
|
+
}
|
39
|
+
|
40
|
+
showWarning() {
|
41
|
+
document.querySelector("dialog.ucb-rails-session-timeout-dialog").showModal()
|
42
|
+
this.startCountdown()
|
43
|
+
}
|
44
|
+
|
45
|
+
startCountdown() {
|
46
|
+
const countdownEl = document.getElementById("session_timeout_countdown")
|
47
|
+
const timeoutAt = Date.now() + (this.warningLengthInSeconds * 1000)
|
48
|
+
|
49
|
+
const updateCountdown = () => {
|
50
|
+
const remainingSeconds = Math.max(0, Math.round((timeoutAt - Date.now()) / 1000))
|
51
|
+
const minutes = Math.floor(remainingSeconds / 60)
|
52
|
+
const seconds = remainingSeconds % 60
|
53
|
+
|
54
|
+
countdownEl.textContent = `${minutes}:${seconds.toString().padStart(2, "0")}`
|
55
|
+
|
56
|
+
if (remainingSeconds <= 0) {
|
57
|
+
clearInterval(this.countdownTimer)
|
58
|
+
if (this.redirectUrl) {
|
59
|
+
window.location.href = this.redirectUrl
|
60
|
+
} else {
|
61
|
+
window.location.reload()
|
62
|
+
}
|
63
|
+
}
|
64
|
+
}
|
65
|
+
|
66
|
+
updateCountdown() // Initial update
|
67
|
+
this.countdownTimer = setInterval(updateCountdown, 1000)
|
68
|
+
}
|
69
|
+
|
70
|
+
extendSession() {
|
71
|
+
fetch(window.location.href).catch(() => {})
|
72
|
+
clearInterval(this.countdownTimer)
|
73
|
+
this.setupTimer()
|
74
|
+
}
|
75
|
+
}
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ucb_rails_session_timeout
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Darin Wilson
|
8
|
+
- Michael Valenza
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2025-02-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '7.0'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '7.0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: railties
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
description: Provides configurable session timeout with warning notifications
|
43
|
+
email:
|
44
|
+
- darinwilson@berkeley.edu
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".rspec"
|
50
|
+
- CHANGELOG.md
|
51
|
+
- CODE_OF_CONDUCT.md
|
52
|
+
- LICENSE.txt
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- app/javascript/ucb_rails_session_timeout/activity_timer.js
|
56
|
+
- lib/ucb_rails_session_timeout.rb
|
57
|
+
- lib/ucb_rails_session_timeout/activity_tracker.rb
|
58
|
+
- lib/ucb_rails_session_timeout/config.rb
|
59
|
+
- lib/ucb_rails_session_timeout/version.rb
|
60
|
+
- lib/ucb_rails_session_timeout/view_helper.rb
|
61
|
+
- sig/ucb_rails_session_timeout.rbs
|
62
|
+
- vendor/assets/javascripts/ucb_rails_session_timeout.js
|
63
|
+
homepage: https://gitlab.com/ucb-bit/cad/aa/ucb_rails_session_timeout
|
64
|
+
licenses:
|
65
|
+
- MIT
|
66
|
+
metadata:
|
67
|
+
homepage_uri: https://gitlab.com/ucb-bit/cad/aa/ucb_rails_session_timeout
|
68
|
+
source_code_uri: https://gitlab.com/ucb-bit/cad/aa/ucb_rails_session_timeout
|
69
|
+
changelog_uri: https://gitlab.com/ucb-bit/cad/aa/ucb_rails_session_timeout/-/blob/main/CHANGELOG.md
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: 3.0.0
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
requirements: []
|
85
|
+
rubygems_version: 3.5.3
|
86
|
+
signing_key:
|
87
|
+
specification_version: 4
|
88
|
+
summary: Auto-logout functionality for Rails applications
|
89
|
+
test_files: []
|