fix: crazy loop of refresh_session
Massimo Melina committed
Jan 19, 2022 at 22:19 UTC
a1c2c331983bc5217e72ca633d6c52f3d54c5184
2 files changed
+9
-11
frontend/src/login.ts
+8
-11
@@ -4,11 +4,7 @@ import { alertDialog } from './dialog'
4
import { SRPClientSession, SRPParameters, SRPRoutines } from 'tssrp6a'
5
import { working } from './misc'
6
7
-let refresher: NodeJS.Timeout
8
-
7
export async function login(username:string, password:string) {
10
- if (refresher)
11
- clearInterval(refresher)
8
const stopWorking = working()
9
try {
10
/* simple login without encryption. Here commented just for example. Please use SRP version.
@@ -48,20 +44,21 @@ export async function login(username:string, password:string) {
44
}
45
46
// @ts-ignore
51
-if (window.SESSION) sessionRefresher(window.SESSION)
47
+sessionRefresher(window.SESSION)
48
53
-function sessionRefresher({ exp, username }:{ exp:string, username:string }) {
49
+function sessionRefresher(response: any) {
50
+ if (!response) return
51
+ const { exp, username } = response
52
state.username = username
55
- if (!exp) return
53
+ if (!username || !exp) return
54
const delta = new Date(exp).getTime() - Date.now()
57
- const every = delta - 30_000
58
- console.debug('session refresh every', Math.round(every/1000))
59
- refresher = setInterval(() => apiCall('refresh_session'), every)
55
+ const t = Math.min(delta - 30_000, 600_000)
56
+ console.debug('session refresh in', Math.round(t/1000))
57
+ setTimeout(() => apiCall('refresh_session').then(sessionRefresher), t)
58
}
59
60
export function logout(){
61
return apiCall('logout').then(()=> {
64
- clearInterval(refresher)
62
state.username = ''
63
})
64
}
src/serveFrontend.ts
+1
@@ -36,6 +36,7 @@ function serveStaticFrontend() : Koa.Middleware {
36
if (path.endsWith('/')) { // we don't cache the index as it's small and may prevent plugins change to apply
37
ctx.body = await treatIndex(ctx, String(await fs.readFile(BASE + 'index.html')))
38
ctx.type = 'html'
39
+ ctx.set('Cache-Control', 'no-store, no-cache, must-revalidate')
40
} else {
41
const fullPath = BASE + path.slice(1)
42
const modifier = path.includes('static/js') ? // webpack