likwid/frontend/src/middleware.ts
Marco Allegretti a61ec18913 backend, compose, docs, +1: modify 13 files
Verified changes:
- modify backend/.env.example
- modify backend/src/api/auth.rs
- modify backend/src/config/mod.rs
- modify backend/src/rate_limit.rs
- modify compose/.env.demo.example
- modify compose/.env.production.example
- modify compose/demo.yml
- modify compose/production.yml
- modify docs/admin/configuration.md
- modify docs/admin/installation.md
- modify docs/admin/opensuse-operator-kit.md
- modify docs/admin/security.md
- modify frontend/src/middleware.ts

Diffstat:
- 13 files changed, 243 insertions(+), 41 deletions(-)
2026-02-24 19:45:42 +01:00

69 lines
1.9 KiB
TypeScript

import { defineMiddleware } from 'astro:middleware';
function isEnabled(v: string | undefined): boolean {
if (!v) return false;
const n = v.trim().toLowerCase();
return n === '1' || n === 'true' || n === 'yes' || n === 'on';
}
const DEMO_COOKIE = 'likwid_demo_unlocked';
export const onRequest = defineMiddleware(async (context, next) => {
const publicDemoSite = isEnabled((globalThis as any).process?.env?.PUBLIC_DEMO_SITE);
if (!publicDemoSite) {
return next();
}
const url = new URL(context.request.url);
const path = url.pathname;
const forwardedProto = context.request.headers.get('x-forwarded-proto')?.trim().toLowerCase();
const isHttps = url.protocol === 'https:' || forwardedProto === 'https';
if (path === '/demo' && url.searchParams.get('enter') === '1') {
let nextPath = url.searchParams.get('next') || '/communities';
if (!nextPath.startsWith('/')) {
nextPath = '/communities';
}
context.cookies.set(DEMO_COOKIE, '1', {
path: '/',
httpOnly: true,
sameSite: 'lax',
secure: isHttps,
maxAge: 60 * 60 * 12,
});
return context.redirect(nextPath);
}
if (path.startsWith('/_astro') || path.startsWith('/favicon') || path.startsWith('/robots')) {
return next();
}
if (path === '/setup' || path === '/register') {
return context.redirect(`/demo?next=${encodeURIComponent(path + url.search)}`);
}
const isProtected =
path === '/login' ||
path === '/dashboard' ||
path === '/delegations' ||
path === '/notifications' ||
path === '/settings' ||
path.startsWith('/admin') ||
path.startsWith('/communities') ||
path.startsWith('/proposals') ||
path.startsWith('/users');
if (!isProtected) {
return next();
}
const unlocked = context.cookies.get(DEMO_COOKIE)?.value === '1';
if (!unlocked) {
return context.redirect(`/demo?next=${encodeURIComponent(path + url.search)}`);
}
return next();
});