update cf context on id
This commit is contained in:
@@ -3,11 +3,14 @@ import type { Route } from './+types/authorize'
|
||||
import { redirect } from 'react-router'
|
||||
import { parse } from 'cookie'
|
||||
|
||||
import { cloudflareContext } from '@repo/auth/context'
|
||||
|
||||
export async function loader({ request, context }: Route.LoaderArgs) {
|
||||
const cloudflare = context.get(cloudflareContext)
|
||||
const cookies = parse(request.headers.get('Cookie') || '')
|
||||
const url = new URL(request.url)
|
||||
const loginUrl = new URL('/', url.origin)
|
||||
const authorizeUrl = new URL('/authorize', context.cloudflare.env.ISSUER_URL)
|
||||
const authorizeUrl = new URL('/authorize', cloudflare.env.ISSUER_URL)
|
||||
authorizeUrl.search = url.search
|
||||
loginUrl.search = url.search
|
||||
|
||||
@@ -16,7 +19,7 @@ export async function loader({ request, context }: Route.LoaderArgs) {
|
||||
}
|
||||
|
||||
if (!url.searchParams.has('client_id')) {
|
||||
throw redirect(context.cloudflare.env.APP_URL)
|
||||
throw redirect(cloudflare.env.APP_URL)
|
||||
}
|
||||
|
||||
const r = await fetch(authorizeUrl.toString(), {
|
||||
|
||||
@@ -7,6 +7,7 @@ import { Link } from 'react-router'
|
||||
import { MailIcon } from 'lucide-react'
|
||||
import { z } from 'zod'
|
||||
|
||||
import { cloudflareContext } from '@repo/auth/context'
|
||||
import logo from '@repo/ui/components/logo2.svg'
|
||||
import { Button } from '@repo/ui/components/ui/button'
|
||||
import {
|
||||
@@ -44,7 +45,8 @@ export function meta({}: Route.MetaArgs) {
|
||||
}
|
||||
|
||||
export async function action({ request, context }: Route.ActionArgs) {
|
||||
const url = new URL('/forgot', context.cloudflare.env.ISSUER_URL)
|
||||
const cloudflare = context.get(cloudflareContext)
|
||||
const url = new URL('/forgot', cloudflare.env.ISSUER_URL)
|
||||
const body = await request.json()
|
||||
|
||||
console.log(url.toString())
|
||||
|
||||
@@ -8,6 +8,7 @@ import { useForm } from 'react-hook-form'
|
||||
import { Link, useFetcher } from 'react-router'
|
||||
import { z } from 'zod'
|
||||
|
||||
import { cloudflareContext } from '@repo/auth/context'
|
||||
import logo from '@repo/ui/components/logo2.svg'
|
||||
import { Button } from '@repo/ui/components/ui/button'
|
||||
import { Checkbox } from '@repo/ui/components/ui/checkbox'
|
||||
@@ -47,7 +48,8 @@ export function meta({}: Route.MetaArgs) {
|
||||
}
|
||||
|
||||
export async function action({ request, context }: Route.ActionArgs) {
|
||||
const url = new URL('/authentication', context.cloudflare.env.ISSUER_URL)
|
||||
const cloudflare = context.get(cloudflareContext)
|
||||
const url = new URL('/authentication', cloudflare.env.ISSUER_URL)
|
||||
const formData = Object.fromEntries(await request.formData())
|
||||
|
||||
try {
|
||||
|
||||
@@ -8,6 +8,7 @@ import { CheckCircle2Icon } from 'lucide-react'
|
||||
import { useForm } from 'react-hook-form'
|
||||
import { redirect, useFetcher } from 'react-router'
|
||||
|
||||
import { cloudflareContext } from '@repo/auth/context'
|
||||
import { Button } from '@repo/ui/components/ui/button'
|
||||
import { Checkbox } from '@repo/ui/components/ui/checkbox'
|
||||
import {
|
||||
@@ -35,7 +36,8 @@ export function meta({}: Route.MetaArgs) {
|
||||
}
|
||||
|
||||
export async function action({ request, context }: Route.ActionArgs) {
|
||||
const url = new URL('/register', context.cloudflare.env.ISSUER_URL)
|
||||
const cloudflare = context.get(cloudflareContext)
|
||||
const url = new URL('/register', cloudflare.env.ISSUER_URL)
|
||||
const body = await request.json()
|
||||
|
||||
const r = await fetch(url.toString(), {
|
||||
|
||||
@@ -22,7 +22,7 @@ import {
|
||||
} from '@repo/ui/components/ui/form'
|
||||
import { Label } from '@repo/ui/components/ui/label'
|
||||
import { Spinner } from '@repo/ui/components/ui/spinner'
|
||||
import { request as req } from '@repo/util/request'
|
||||
import { cloudflareContext } from '@repo/auth/context'
|
||||
import {
|
||||
Alert,
|
||||
AlertDescription,
|
||||
@@ -50,8 +50,8 @@ export function meta({}: Route.MetaArgs) {
|
||||
}
|
||||
|
||||
export async function action({ params, request, context }: Route.ActionArgs) {
|
||||
const { token } = params
|
||||
const url = new URL(`/reset/${token}`, context.cloudflare.env.ISSUER_URL)
|
||||
const cloudflare = context.get(cloudflareContext)
|
||||
const url = new URL(`/reset/${params.token}`, cloudflare.env.ISSUER_URL)
|
||||
const body = await request.json()
|
||||
|
||||
const r = await fetch(url.toString(), {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import type { Route } from './+types/upstream'
|
||||
|
||||
import { cloudflareContext } from '@repo/auth/context'
|
||||
|
||||
export const loader = proxy
|
||||
export const action = proxy
|
||||
|
||||
@@ -9,8 +11,9 @@ async function proxy({
|
||||
request,
|
||||
context
|
||||
}: Route.ActionArgs): Promise<Response> {
|
||||
const cloudflare = context.get(cloudflareContext)
|
||||
const { pathname, search } = new URL(request.url)
|
||||
const url = new URL(pathname + search, context.cloudflare.env.ISSUER_URL)
|
||||
const url = new URL(pathname + search, cloudflare.env.ISSUER_URL)
|
||||
const headers = new Headers(request.headers)
|
||||
|
||||
const shouldCache =
|
||||
@@ -44,7 +47,7 @@ async function proxy({
|
||||
})
|
||||
|
||||
// Store asynchronously (don’t block response)
|
||||
context.cloudflare.ctx.waitUntil(cache.put(cacheKey, cacheResponse))
|
||||
cloudflare.ctx.waitUntil(cache.put(cacheKey, cacheResponse))
|
||||
}
|
||||
|
||||
const contentType = response.headers.get('content-type') || ''
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
import { createRequestHandler, RouterContextProvider } from 'react-router'
|
||||
import { cloudflareContext } from '@repo/auth/context'
|
||||
|
||||
declare module 'react-router' {
|
||||
export interface AppLoadContext {
|
||||
cloudflare: {
|
||||
env: Env
|
||||
ctx: ExecutionContext
|
||||
}
|
||||
declare global {
|
||||
interface CacheStorage {
|
||||
default: Cache
|
||||
}
|
||||
}
|
||||
|
||||
declare module '@repo/auth/context' {
|
||||
interface CloudflareEnv extends Env {}
|
||||
interface CloudflareCtx extends ExecutionContext {}
|
||||
}
|
||||
|
||||
const requestHandler = createRequestHandler(
|
||||
() => import('virtual:react-router/server-build'),
|
||||
import.meta.env.MODE
|
||||
@@ -16,11 +19,10 @@ const requestHandler = createRequestHandler(
|
||||
|
||||
export default {
|
||||
async fetch(request, env, ctx) {
|
||||
const context = new RouterContextProvider()
|
||||
|
||||
return requestHandler(
|
||||
request,
|
||||
Object.assign(context, { cloudflare: { env, ctx } })
|
||||
const context = new RouterContextProvider(
|
||||
new Map([[cloudflareContext, { env, ctx }]])
|
||||
)
|
||||
|
||||
return requestHandler(request, context)
|
||||
}
|
||||
} satisfies ExportedHandler<Env>
|
||||
|
||||
Reference in New Issue
Block a user