add turborepo
This commit is contained in:
69
apps/id.saladeaula.digital/app/routes/authorize.ts
Normal file
69
apps/id.saladeaula.digital/app/routes/authorize.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import type { Route } from './+types'
|
||||
|
||||
import { parse } from 'cookie'
|
||||
|
||||
export const OK = 200
|
||||
export const FOUND = 302
|
||||
export const INTERNAL_SERVER_ERROR = 500
|
||||
|
||||
export async function loader({ request, context }: Route.LoaderArgs) {
|
||||
const cookies = parse(request.headers.get('Cookie') || '')
|
||||
const url = new URL(request.url)
|
||||
const loginUrl = new URL('/', url.origin)
|
||||
const issuerUrl = new URL('/authorize', context.cloudflare.env.ISSUER_URL)
|
||||
issuerUrl.search = url.search
|
||||
loginUrl.search = url.search
|
||||
|
||||
if (!cookies?.__session) {
|
||||
return new Response(null, {
|
||||
status: FOUND,
|
||||
headers: {
|
||||
Location: loginUrl.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'
|
||||
})
|
||||
|
||||
if (r.status === FOUND) {
|
||||
return new Response(await r.text(), {
|
||||
status: r.status,
|
||||
headers: r.headers
|
||||
})
|
||||
}
|
||||
|
||||
console.log('Authorize response', {
|
||||
json: await r.json(),
|
||||
headers: r.headers,
|
||||
status: r.status
|
||||
})
|
||||
|
||||
// Deny authorization if user lacks scopes requested by client
|
||||
if (r.status === FOUND) {
|
||||
return new Response(null, {
|
||||
status: r.status,
|
||||
headers: {
|
||||
Location: new URL('/deny', url.origin).toString()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return new Response(null, {
|
||||
status: FOUND,
|
||||
headers: {
|
||||
Location: loginUrl.toString()
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
return new Response(null, { status: INTERNAL_SERVER_ERROR })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user