Skip to content

Instantly share code, notes, and snippets.

@readysetawesome
Created March 14, 2023 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save readysetawesome/80a3d95dcacf5c0cd1e086b005b370d5 to your computer and use it in GitHub Desktop.
Save readysetawesome/80a3d95dcacf5c0cd1e086b005b370d5 to your computer and use it in GitHub Desktop.
patch to allow setting cloudflare pages access aud from env vars
diff --git a/functions/_middleware.ts b/functions/_middleware.ts
index 9d5ae42..8f46b34 100644
--- a/functions/_middleware.ts
+++ b/functions/_middleware.ts
@@ -1,8 +1,20 @@
import cloudflareAccessPlugin from "@cloudflare/pages-plugin-cloudflare-access";
-export const onRequest = ({ env }) => {
- return cloudflareAccessPlugin({
- domain: "https://timely-tasker.cloudflareaccess.com",
- aud: env.AUDIENCE,
- });
+interface Env {
+ AUDIENCE: string;
}
+
+export const onRequest: PagesFunction<Env> = (context) => {
+ const aud = context.env.AUDIENCE;
+ if (typeof aud !== 'string') {
+ // must be local dev mode, just pass through
+ // TODO: Stub local dev user auth in the middeware/functions
+ return context.next();
+ } else {
+ return cloudflareAccessPlugin({
+ domain: "https://timely-tasker.cloudflareaccess.com",
+ aud: aud
+ })(context);
+ }
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment