import { json, redirect, type LoaderFunctionArgs, type TypedResponse } from '@remix-run/cloudflare'; import { isAuthenticated, type Session } from './sessions'; type RequestArgs = Pick; export async function loadWithAuth( args: T, handler: (args: T, session: Session) => Promise, ) { return handleWithAuth(args, handler, (response) => redirect('/login', response)); } export async function actionWithAuth( args: T, handler: (args: T, session: Session) => Promise, ) { return await handleWithAuth(args, handler, (response) => json({}, { status: 401, ...response })); } async function handleWithAuth( args: T, handler: (args: T, session: Session) => Promise, fallback: (partial: ResponseInit) => R, ) { const { request, context } = args; const { session, response } = await isAuthenticated(request, context.cloudflare.env); if (session == null && !import.meta.env.VITE_DISABLE_AUTH) { return fallback(response); } const handlerResponse = await handler(args, session || {}); if (response) { for (const [key, value] of Object.entries(response.headers)) { handlerResponse.headers.append(key, value); } } return handlerResponse; }