headplane/app/layouts/dashboard.tsx

50 lines
1.3 KiB
TypeScript
Raw Normal View History

import { type LoaderFunctionArgs, Outlet, redirect } from 'react-router';
import { ErrorPopup } from '~/components/Error';
2025-03-22 01:36:27 -04:00
import type { LoadContext } from '~/server';
import { pruneEphemeralNodes } from '~/server/db/pruner';
import ResponseError from '~/server/headscale/api-error';
2025-03-22 01:36:27 -04:00
import log from '~/utils/log';
2024-03-25 17:51:11 -04:00
2025-03-22 01:36:27 -04:00
export async function loader({
request,
context,
...rest
2025-03-22 01:36:27 -04:00
}: LoaderFunctionArgs<LoadContext>) {
const healthy = await context.client.healthcheck();
const session = await context.sessions.auth(request);
await pruneEphemeralNodes({ context, request, ...rest });
// We shouldn't session invalidate if Headscale is down
// TODO: Notify in the logs or the UI that OIDC auth key is wrong if enabled
if (healthy) {
try {
await context.client.get('v1/apikey', session.api_key);
} catch (error) {
2025-03-22 01:36:27 -04:00
if (error instanceof ResponseError) {
log.debug('api', 'API Key validation failed %o', error);
return redirect('/login', {
headers: {
'Set-Cookie': await context.sessions.destroySession(),
},
});
}
}
}
2024-03-30 02:44:06 -04:00
return {
healthy,
2025-01-28 16:06:41 -05:00
};
2024-03-25 17:51:11 -04:00
}
export default function Layout() {
return (
<main className="container mx-auto overscroll-contain mt-4 mb-24">
<Outlet />
</main>
2024-12-31 10:30:14 +05:30
);
2024-03-29 16:14:35 -04:00
}
export function ErrorBoundary() {
return <ErrorPopup type="embedded" />;
}