fix: handle apikey expiration errors gracefully

This commit is contained in:
Aarnav Tale
2024-03-26 09:04:27 -04:00
parent abaf71636e
commit ed9cc23020
2 changed files with 33 additions and 3 deletions
+21 -1
View File
@@ -3,7 +3,8 @@ import { type LoaderFunctionArgs, redirect } from '@remix-run/node'
import { Outlet } from '@remix-run/react'
import TabLink from '~/components/TabLink'
import { getSession } from '~/utils/sessions'
import { HeadscaleError, pull } from '~/utils/headscale'
import { destroySession, getSession } from '~/utils/sessions'
export async function loader({ request }: LoaderFunctionArgs) {
const session = await getSession(request.headers.get('Cookie'))
@@ -11,6 +12,25 @@ export async function loader({ request }: LoaderFunctionArgs) {
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) {
console.error(error)
// 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
}
// eslint-disable-next-line unicorn/no-null
return null
}