From aac1e4a0d254c6499627c09f3d861d18d98c3eaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Rafael=20Siqueira?= Date: Sun, 17 Aug 2025 20:05:29 -0300 Subject: [PATCH] add token --- .../client/app/routes/authorize.ts | 39 +++++++++++++++++++ .../client/app/routes/token.ts | 15 +++++++ 2 files changed, 54 insertions(+) create mode 100644 id.saladeaula.digital/client/app/routes/authorize.ts create mode 100644 id.saladeaula.digital/client/app/routes/token.ts 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 + }) +}