add token

This commit is contained in:
2025-08-17 20:05:29 -03:00
parent 3fc315b5fa
commit aac1e4a0d2
2 changed files with 54 additions and 0 deletions

View File

@@ -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 })
}
}

View File

@@ -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
})
}