frontend can now avoid initial call to refresh_session
Massimo Melina committed
Jan 15, 2022 at 21:01 UTC
9382e7f72a864ea164b619ba7119f70028923ee3
5 files changed
+14
-11
frontend/public/index.html
+1
@@ -7,6 +7,7 @@
7
<meta name="theme-color" content="#000000" />
8
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
9
<link href="fontello.css" rel="stylesheet" />
10
+ <script>SESSION = _HFS_SESSION_</script>
11
<title>File Server</title>
12
</head>
13
<body>
frontend/src/login.ts
+2
-2
@@ -47,8 +47,8 @@ export async function login(username:string, password:string) {
47
finally { stopWorking() }
48
}
49
50
-apiCall('refresh_session').then(sessionRefresher, ()=>{})
51
- .finally(()=> state.restoringSession=false)
50
+// @ts-ignore
51
+if (window.SESSION) sessionRefresher(window.SESSION)
52
53
function sessionRefresher({ exp, username }:{ exp:string, username:string }) {
54
state.username = username
frontend/src/state.ts
-2
@@ -14,7 +14,6 @@ export const state = proxy<{
14
invertOrder: boolean,
15
foldersFirst: boolean,
16
theme: string,
17
- restoringSession: boolean,
17
}>({
18
iconsClass: '',
19
username: '',
@@ -25,7 +24,6 @@ export const state = proxy<{
24
invertOrder: false,
25
foldersFirst: true,
26
theme: '',
28
- restoringSession: true,
27
})
28
29
export function useSnapState() {
frontend/src/useFetchList.ts
+2
-3
@@ -18,10 +18,9 @@ export default function useFetchList() {
18
const { sortBy, invertOrder, foldersFirst } = snap
19
useEffect(()=>{
20
setList(sort(list))
21
- }, [sortBy, invertOrder, foldersFirst])
21
+ }, [sortBy, invertOrder, foldersFirst]) //eslint-disable-line
22
23
useEffect(()=>{
24
- if (snap.restoringSession) return // we need this to avoid double file_list just after session is restored at start (happens on slow connections)
24
if (!desiredPath.endsWith('/')) { // useful only in dev, while accessing the frontend directly without passing by the main server
25
window.location.href = window.location.href + '/'
26
return
@@ -74,7 +73,7 @@ export default function useFetchList() {
73
state.stopSearch = undefined
74
src.close()
75
}
77
- }, [desiredPath, search, snap.restoringSession, snap.username, forcer])
76
+ }, [desiredPath, search, snap.username, forcer])
77
return {
78
list, loading, error,
79
reload() {
src/serveFrontend.ts
+9
-4
@@ -4,6 +4,8 @@ import fs from 'fs/promises'
4
import { DEV, FRONTEND_URI, METHOD_NOT_ALLOWED, NO_CONTENT, PLUGINS_PUB_URI } from './const'
5
import { serveFile } from './serveFile'
6
import { mapPlugins } from './plugins'
7
+import { refresh_session } from './api.auth'
8
+import { ApiError } from './apis'
9
10
export const serveFrontend = DEV ? serveProxyFrontend() : serveStaticFrontend()
11
@@ -12,8 +14,8 @@ function serveProxyFrontend() {
14
return proxy('localhost:3000', {
15
filter: ctx => ctx.method === 'GET' || (ctx.status = METHOD_NOT_ALLOWED) && false,
16
proxyReqPathResolver: (ctx) => ctx.path.endsWith('/') ? '/' : ctx.path,
15
- userResDecorator: (res, data, req) => {
16
- return req.url.endsWith('/') ? treatIndex(data.toString('utf8'))
17
+ userResDecorator(res, data, ctx) {
18
+ return ctx.url.endsWith('/') ? treatIndex(ctx, data.toString('utf8'))
19
: data
20
}
21
})
@@ -31,7 +33,7 @@ function serveStaticFrontend() : Koa.Middleware {
33
if (method !== 'GET')
34
return ctx.status = METHOD_NOT_ALLOWED
35
if (path.endsWith('/')) { // we don't cache the index as it's small and may prevent plugins change to apply
34
- ctx.body = treatIndex(String(await fs.readFile(BASE + 'index.html')))
36
+ ctx.body = await treatIndex(ctx, String(await fs.readFile(BASE + 'index.html')))
37
ctx.type = 'html'
38
} else {
39
const fullPath = BASE + path.slice(1)
@@ -48,8 +50,11 @@ function replaceFrontEndRes(body: string) {
50
return body.replace(/((?:src|href) *= *['"])\/?(?![a-z]+:\/\/)/g, '$1'+FRONTEND_URI)
51
}
52
51
-function treatIndex(body: string) {
53
+async function treatIndex(ctx: Koa.Context, body: string) {
54
+ const session = await refresh_session({}, ctx)
55
+ ctx.set('etag', '')
56
return replaceFrontEndRes(body)
57
+ .replace('_HFS_SESSION_', session instanceof ApiError ? 'null' : JSON.stringify(session))
58
// replacing this text allow us to avoid injecting in frontends that don't support plugins. Don't use a <--comment--> or it will be removed by webpack
59
.replace('_HFS_PLUGINS_', pluginsInjection)
60
}