(dev) support login for protected resources while on proxied-frontend mode
Massimo Melina committed
Aug 5, 2022 at 00:12 UTC
b714b63a4c0b177dd793b51b2abbc58b10b23baf
2 files changed
+10
-5
server/src/middlewares.ts
+1
@@ -82,6 +82,7 @@ export const serveGuiAndSharedFiles: Koa.Middleware = async (ctx, next) => {
82
const browserDetected = ctx.get('Upgrade-Insecure-Requests') || ctx.get('Sec-Fetch-Mode') // ugh, heuristics
83
if (!browserDetected) // we don't want to trigger basic authentication on browsers, it's meant for download managers only
84
ctx.set('WWW-Authenticate', 'Basic') // we support basic authentication
85
+ ctx.state.serveApp = true
86
return serveFrontendFiles(ctx, next)
87
}
88
ctx.set({ server:'HFS '+BUILD_TIMESTAMP })
server/src/serveGuiFiles.ts
+9
-5
@@ -24,9 +24,8 @@ function serveStatic(uri: string): Koa.Middleware {
24
}
25
if (ctx.method !== 'GET')
26
return ctx.status = METHOD_NOT_ALLOWED
27
- const loginRequired = ctx.status === UNAUTHORIZED
28
- const serveApp = ctx.path.endsWith('/') || loginRequired
29
- const fullPath = path.join(__dirname, '..', DEV_STATIC, folder, serveApp? '/index.html': ctx.path)
27
+ const serveApp = shouldServeApp(ctx)
28
+ const fullPath = path.join(__dirname, '..', DEV_STATIC, folder, serveApp ? '/index.html': ctx.path)
29
const content = await getOrSet(cache, ctx.path, async () => {
30
const data = await fs.readFile(fullPath).catch(() => null)
31
return serveApp || !data ? data : adjustWebpackLinks(ctx.path, uri, data)
@@ -42,6 +41,10 @@ function serveStatic(uri: string): Koa.Middleware {
41
}
42
}
43
44
+function shouldServeApp(ctx: Koa.Context) {
45
+ return ctx.state.serveApp ||= ctx.path.endsWith('/')
46
+}
47
+
48
function adjustWebpackLinks(path: string, uri: string, data: string | Buffer) {
49
return path.startsWith('/static/js') // webpack
50
? String(data).replace(/(")(static\/)/g, '$1' + uri.substring(1) + '$2')
@@ -65,9 +68,10 @@ function serveProxied(port: string | undefined, uri: string) { // used for devel
68
let proxy: Koa.Middleware
69
import('koa-better-http-proxy').then(lib => // dynamic import to avoid having this in final distribution
70
proxy = lib.default('127.0.0.1:'+port, {
68
- proxyReqPathResolver: (ctx) => ctx.path.endsWith('/') ? '/' : ctx.path,
71
+ proxyReqPathResolver: (ctx) =>
72
+ shouldServeApp(ctx) ? '/' : ctx.path,
73
userResDecorator(res, data, ctx) {
70
- return ctx.path.endsWith('/') ? treatIndex(ctx, String(data), uri)
74
+ return shouldServeApp(ctx) ? treatIndex(ctx, String(data), uri)
75
: adjustWebpackLinks(ctx.path, uri, data)
76
}
77
}) )