fix: each login was not replacing but adding a timer for the session
Massimo Melina committed
Sep 7, 2024 at 15:27 UTC
7468e3102ab02cba91252c3ab6df9014365d7481
3 files changed
+13
-8
admin/src/LoginRequired.ts
+2
-3
@@ -69,8 +69,7 @@ async function login(username: string, password: string) {
69
70
// login was successful, update state
71
state.loginRequired = false
72
- sessionRefresher(res)
72
+ refreshSession(res)
73
}
74
75
-const sessionRefresher = makeSessionRefresher(state)
76
-sessionRefresher(getHFS().session)
75
+const refreshSession = makeSessionRefresher(state)
frontend/src/login.ts
+2
-3
@@ -16,7 +16,7 @@ async function login(username:string, password:string) {
16
const stopWorking = working()
17
return srpClientSequence(username, password, apiCall).then(res => {
18
stopWorking()
19
- sessionRefresher(res)
19
+ refreshSession(res)
20
state.loginRequired = false
21
return res
22
}, (err: any) => {
@@ -28,8 +28,7 @@ async function login(username:string, password:string) {
28
})
29
}
30
31
-const sessionRefresher = makeSessionRefresher(state)
32
-sessionRefresher(getHFS().session)
31
+const refreshSession = makeSessionRefresher(state)
32
33
export function logout() {
34
return apiCall('logout', {}, { modal: working }).catch(res => {
shared/index.ts
+9
-2
@@ -104,15 +104,22 @@ export function getPrefixUrl() {
104
}
105
106
export function makeSessionRefresher(state: any) {
107
- return function sessionRefresher(response: any) {
107
+ let timeout: any
108
+ const initial = getHFS().session
109
+ refreshSession(initial)
110
+ return refreshSession
111
+
112
+ function refreshSession(response: any) {
113
if (!response) return
114
const { exp } = response
115
+ Object.assign(initial, response) // keep it updated, not necessary, just in case someone is looking at this instead of the state
116
Object.assign(state, _.pick(response, ['username', 'adminUrl', 'canChangePassword', 'accountExp']))
117
if (!response.username || !exp) return
118
const delta = new Date(exp).getTime() - Date.now()
119
const t = _.clamp(delta - 30_000, 4_000, 600_000)
120
console.debug('session refresh in', Math.round(t / 1000))
115
- setTimeout(() => apiCall('refresh_session').then(sessionRefresher), t)
121
+ clearTimeout(timeout)
122
+ timeout = setTimeout(() => apiCall('refresh_session').then(refreshSession), t)
123
}
124
}
125