@samitouri / QOSami-HFS / commits / 2bb03187

better code: shared

Massimo Melina committed Jun 23, 2023 at 15:59 UTC 2bb03187ab1b50252193fcf340c8a6f168d4c1f9
3 files changed +20 -27
admin/src/LoginRequired.ts
+2 -12
@@ -2,7 +2,7 @@
2
3 import { state, useSnapState } from './state'
4 import { createElement as h, Fragment, useEffect, useRef, useState } from 'react'
5 -import { Center, getHFS } from './misc'
5 +import { Center, getHFS, makeSessionRefresher } from './misc'
6 import { Form } from '@hfs/mui-grid-form'
7 import { apiCall } from './api'
8 import { srpSequence } from '@hfs/shared'
@@ -71,15 +71,5 @@ async function login(username: string, password: string) {
71 sessionRefresher(res)
72 }
73
74 +const sessionRefresher = makeSessionRefresher(state)
75 sessionRefresher(getHFS().session)
75 -
76 -function sessionRefresher(response: any) {
77 - if (!response) return
78 - const { exp, username } = response
79 - state.username = username
80 - if (!username || !exp) return
81 - const delta = new Date(exp).getTime() - Date.now()
82 - const t = Math.min(delta - 30_000, 600_000)
83 - console.debug('session refresh in', Math.round(t/1000))
84 - setTimeout(() => apiCall('refresh_session').then(sessionRefresher), t)
85 -}
frontend/src/login.ts
+2 -14
@@ -3,13 +3,12 @@
3 import { apiCall } from '@hfs/shared/api'
4 import { state, useSnapState } from './state'
5 import { alertDialog, newDialog } from './dialog'
6 -import { getHFS, getPrefixUrl, hIcon, srpSequence, working } from './misc'
6 +import { getHFS, getPrefixUrl, hIcon, makeSessionRefresher, srpSequence, working } from './misc'
7 import { useNavigate } from 'react-router-dom'
8 import { createElement as h, Fragment, useEffect, useRef } from 'react'
9 import { t, useI18N } from './i18n'
10 import { reloadList } from './useFetchList'
11 import { CustomCode } from './components'
12 -import _ from 'lodash'
12
13 async function login(username:string, password:string) {
14 const stopWorking = working()
@@ -28,20 +27,9 @@ async function login(username:string, password:string) {
27 })
28 }
29
30 +const sessionRefresher = makeSessionRefresher(state)
31 sessionRefresher(getHFS().session)
32
33 -function sessionRefresher(response: any) {
34 - if (!response) return
35 - const { exp, username, adminUrl } = response
36 - state.username = username
37 - state.adminUrl = adminUrl
38 - if (!username || !exp) return
39 - const delta = new Date(exp).getTime() - Date.now()
40 - const t = _.clamp(delta - 30_000, 5_000, 600_000)
41 - console.debug('session refresh in', Math.round(t/1000))
42 - setTimeout(() => apiCall('refresh_session').then(sessionRefresher), t)
43 -}
44 -
33 export function logout(){
34 return apiCall('logout').catch(res => {
35 if (res.code !== 401) // we expect 401
shared/index.ts
+16 -1
@@ -1,6 +1,7 @@
1 // This file is part of HFS - Copyright 2021-2023, Massimo Melina <a@rejetto.com> - License https://www.gnu.org/licenses/gpl-3.0.txt
2
3 import _ from 'lodash'
4 +import { apiCall } from './api'
5 export * from './react'
6 export * from './dialogs'
7 export * from './srp'
@@ -187,4 +188,18 @@ export function getPrefixUrl() {
188
189 export function basename(path: string) {
190 return path.slice(path.lastIndexOf('/') + 1 || path.lastIndexOf('\\') + 1)
190 -}
\ No newline at end of file
191 +}
192 +
193 +export function makeSessionRefresher(state: any) {
194 + return function sessionRefresher(response: any) {
195 + if (!response) return
196 + const { exp, username, adminUrl } = response
197 + state.username = username
198 + state.adminUrl = adminUrl
199 + if (!username || !exp) return
200 + const delta = new Date(exp).getTime() - Date.now()
201 + const t = _.clamp(delta - 30_000, 4_000, 600_000)
202 + console.debug('session refresh in', Math.round(t / 1000))
203 + setTimeout(() => apiCall('refresh_session').then(sessionRefresher), t)
204 + }
205 +}