headplane/app/routes/_data.tsx

63 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-03-25 17:51:11 -04:00
import { type LoaderFunctionArgs, redirect } from '@remix-run/node'
import { Outlet, useLoaderData } from '@remix-run/react'
2024-03-25 17:51:11 -04:00
2024-03-29 16:14:35 -04:00
import { ErrorPopup } from '~/components/Error'
import Header from '~/components/Header'
2024-03-30 01:48:35 -04:00
import { getContext } from '~/utils/config'
import { HeadscaleError, pull } from '~/utils/headscale'
import { destroySession, getSession } from '~/utils/sessions'
2024-03-25 17:51:11 -04:00
export async function loader({ request }: LoaderFunctionArgs) {
const session = await getSession(request.headers.get('Cookie'))
if (!session.has('hsApiKey')) {
return redirect('/login')
}
try {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await pull('v1/apikey', session.get('hsApiKey')!)
} catch (error) {
if (error instanceof HeadscaleError) {
// Safest to just redirect to login if we can't pull
return redirect('/login', {
headers: {
// eslint-disable-next-line @typescript-eslint/naming-convention
'Set-Cookie': await destroySession(session)
}
})
}
// Otherwise propagate to boundary
throw error
}
2024-03-30 01:48:35 -04:00
const context = await getContext()
2024-03-30 02:44:06 -04:00
return {
...context,
user: session.get('user')
}
2024-03-25 17:51:11 -04:00
}
export default function Layout() {
2024-03-30 01:48:35 -04:00
const data = useLoaderData<typeof loader>()
2024-03-25 17:51:11 -04:00
return (
<>
<Header data={data}/>
2024-03-25 17:51:11 -04:00
<main className='container mx-auto overscroll-contain mt-4 mb-24'>
2024-03-25 17:51:11 -04:00
<Outlet/>
</main>
</>
)
}
2024-03-29 16:14:35 -04:00
export function ErrorBoundary() {
return (
<>
<Header/>
2024-03-29 16:14:35 -04:00
<ErrorPopup type='embedded'/>
</>
)
}