Skip to content

Commit 3672831

Browse files
committed
Add password reset countdown
1 parent 485014c commit 3672831

File tree

3 files changed

+53
-13
lines changed

3 files changed

+53
-13
lines changed

static/js/password-change.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
document.addEventListener("DOMContentLoaded", () => {
2+
const countdownEl = document.getElementById("reset-password-countdown");
3+
if (!countdownEl) return;
4+
5+
startCountdown(countdownEl);
6+
});
7+
8+
function startCountdown(element) {
9+
const token = new URLSearchParams(window.location.search).get("sig");
10+
const payload = JSON.parse(atob(token.split(".")[1].replace(/-/g, "+").replace(/_/g, "/")));
11+
12+
const BUFFER_MS = 1000;
13+
const exp = payload.exp * 1000 + BUFFER_MS;
14+
15+
const interval = setInterval(() => updateCountdown(element, exp, interval), 1000);
16+
17+
updateCountdown(element, exp, interval);
18+
}
19+
20+
function updateCountdown(element, exp, interval) {
21+
const remaining = Math.max(0, Math.floor((exp - Date.now()) / 1000));
22+
const minutes = Math.floor(remaining / 60);
23+
const seconds = remaining % 60;
24+
25+
element.textContent = `${String(minutes).padStart(2,"0")}:${String(seconds).padStart(2,"0")}`;
26+
27+
if (remaining <= 0) {
28+
element.textContent = "00:00";
29+
clearInterval(interval);
30+
setTimeout(() => location.reload(), 50);
31+
}
32+
}

templates/auth.html

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
{% block title %}Sign In{% endblock %}
66

7-
{% block extra_styles %}{% endblock %}
8-
9-
{% block main_info %}
10-
<link
11-
href="{{ relative_path_for('static', path='/css/auth.css') }}"
12-
rel="stylesheet"
13-
/>
7+
{% block extra_styles %}
8+
<link
9+
href="{{ relative_path_for('static', path='/css/auth.css') }}"
10+
rel="stylesheet"
11+
/>
1412
{% endblock %}
1513

14+
{% block main_info %}{% endblock %}
15+
1616
{% block main_content %}
1717
<div
1818
class="container"

templates/password_change.html

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
{% block title %}Password Reset{% endblock %}
66

7-
{% block main_info %}
8-
<link
9-
href="{{ relative_path_for('static', path='/css/auth.css') }}"
10-
rel="stylesheet"
11-
/>
7+
{% block extra_styles %}
8+
<link
9+
href="{{ relative_path_for('static', path='/css/auth.css') }}"
10+
rel="stylesheet"
11+
/>
1212
{% endblock %}
1313

14+
{% block main_info %}{% endblock %}
15+
1416
{% block main_content %}
1517
<div
1618
class="container"
@@ -83,7 +85,7 @@
8385
<p
8486
class="auth-warning"
8587
>
86-
The link is valid for 15 minutes.
88+
This reset link expires in <span id="reset-password-countdown">15:00</span>.
8789
</p>
8890
{% else %}
8991
<p
@@ -101,3 +103,9 @@
101103
</div>
102104
</div>
103105
{% endblock %}
106+
107+
{% block extra_scripts %}
108+
<script
109+
src="{{ relative_path_for('static', path='/js/password-change.js') }}"
110+
></script>
111+
{% endblock %}

0 commit comments

Comments
 (0)