add router

This commit is contained in:
2025-08-17 20:05:15 -03:00
parent f3a332da8d
commit 3fc315b5fa
4 changed files with 7 additions and 56 deletions

View File

@@ -1,39 +0,0 @@
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: {
'Content-Type': 'application/json',
Cookie: request.headers.get('Cookie')
},
redirect: 'manual'
})
return new Response(await r.text(), {
status: r.status,
headers: r.headers
})
} catch {
return new Response(null, { status: 500 })
}
}

View File

@@ -30,13 +30,11 @@ export function meta({}: Route.MetaArgs) {
}
export async function action({ request, context }: Route.ActionArgs) {
const issuerUrl = context.cloudflare.env.ISSUER_URL
const issuerUrl = new URL('/session', context.cloudflare.env.ISSUER_URL)
const formData = Object.fromEntries(await request.formData())
const url = new URL(request.url)
url.pathname = '/authorize'
try {
const r = await fetch(`${issuerUrl}/session`, {
const r = await fetch(issuerUrl.toString(), {
method: 'POST',
headers: {
'Content-Type': 'application/json'
@@ -44,6 +42,9 @@ export async function action({ request, context }: Route.ActionArgs) {
body: JSON.stringify(formData)
})
const url = new URL(request.url)
url.pathname = '/authorize'
const headers = new Headers(r.headers)
headers.set('Location', url.toString())