mirror of
https://github.com/tale/headplane.git
synced 2026-08-19 17:36:19 +00:00
d3d7c7cc0e
This also ships with a better error page
48 lines
1.0 KiB
TypeScript
48 lines
1.0 KiB
TypeScript
import { createCookie, type LoaderFunctionArgs, redirect } from 'react-router';
|
|
import type { LoadContext } from '~/server';
|
|
import { beginAuthFlow, getRedirectUri } from '~/utils/oidc';
|
|
|
|
export async function loader({
|
|
request,
|
|
context,
|
|
}: LoaderFunctionArgs<LoadContext>) {
|
|
try {
|
|
await context.sessions.auth(request);
|
|
return redirect('/machines');
|
|
} catch {}
|
|
|
|
if (
|
|
!context.oidc ||
|
|
typeof context.oidc === 'string' ||
|
|
!context.config.oidc
|
|
) {
|
|
throw new Error('OIDC is not enabled');
|
|
}
|
|
|
|
const cookie = createCookie('__oidc_auth_flow', {
|
|
httpOnly: true,
|
|
maxAge: 300, // 5 minutes
|
|
});
|
|
|
|
const redirectUri =
|
|
context.config.oidc?.redirect_uri ?? getRedirectUri(request);
|
|
const data = await beginAuthFlow(
|
|
context.oidc,
|
|
redirectUri,
|
|
context.config.oidc.scope,
|
|
context.config.oidc.extra_params,
|
|
);
|
|
|
|
return redirect(data.url, {
|
|
status: 302,
|
|
headers: {
|
|
'Set-Cookie': await cookie.serialize({
|
|
state: data.state,
|
|
nonce: data.nonce,
|
|
code_verifier: data.codeVerifier,
|
|
redirect_uri: redirectUri,
|
|
}),
|
|
},
|
|
});
|
|
}
|