diff --git a/id.saladeaula.digital/client/app/routes/authorize.ts b/id.saladeaula.digital/client/app/routes/authorize.ts new file mode 100644 index 0000000..8516a7c --- /dev/null +++ b/id.saladeaula.digital/client/app/routes/authorize.ts @@ -0,0 +1,39 @@ +import { parse } from 'cookie' + +import type { Route } from './+types/authorize' + +export async function loader({ request, context }: Route.LoaderArgs) { + const cookies = parse(request.headers.get('Cookie') || '') + const url = new URL(request.url) + const issuerUrl = new URL('/authorize', context.cloudflare.env.ISSUER_URL) + issuerUrl.search = url.search + + if (!cookies.session_id) { + url.pathname = '/' + + return new Response(null, { + status: 302, + headers: { + Location: url.toString() + } + }) + } + + try { + const r = await fetch(issuerUrl.toString(), { + method: 'GET', + headers: new Headers([ + ['Content-Type', 'application/json'], + ['Cookie', request.headers.get('Cookie') as string] + ]), + redirect: 'manual' + }) + + return new Response(await r.text(), { + status: r.status, + headers: r.headers + }) + } catch { + return new Response(null, { status: 500 }) + } +} diff --git a/id.saladeaula.digital/client/app/routes/token.ts b/id.saladeaula.digital/client/app/routes/token.ts new file mode 100644 index 0000000..c54d606 --- /dev/null +++ b/id.saladeaula.digital/client/app/routes/token.ts @@ -0,0 +1,15 @@ +import type { Route } from './+types/token' + +export async function action({ request, context }: Route.ActionArgs) { + const issuerUrl = new URL('/token', context.cloudflare.env.ISSUER_URL) + const r = await fetch(issuerUrl.toString(), { + method: request.method, + headers: request.headers, + body: await request.text() + }) + + return new Response(await r.text(), { + status: r.status, + headers: r.headers + }) +}