diff --git a/src/middleware.ts b/src/middleware.ts index f7cae2d..8e45015 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -1,11 +1,21 @@ import { clerkMiddleware, createRouteMatcher, clerkClient } from '@clerk/astro/server'; +import type { MiddlewareNext } from 'astro'; +import 'dotenv/config'; + +declare global { + namespace App { + interface Locals { + canAddInventory: boolean; + } + } +} const isProtectedRoute = createRouteMatcher(['/pokemon']); const isAdminRoute = createRouteMatcher(['/admin']); const TARGET_ORG_ID = "org_3Baav9czkRLLlC7g89oJWqRRulK"; -export const onRequest = clerkMiddleware(async (auth, context) => { +export const onRequest = clerkMiddleware(async (auth, context, next) => { const { isAuthenticated, userId, redirectToSignIn, has } = auth(); if (!isAuthenticated && isProtectedRoute(context.request)) { @@ -14,16 +24,18 @@ export const onRequest = clerkMiddleware(async (auth, context) => { // ── Inventory visibility check ────────────────────────────────────────────── // Resolves to true if the user belongs to the target org OR has the feature - const canAddInventory = - isAuthenticated && - userId && + const canAddInventory = process.env.INVENTORY_ACCESS === 'true' || ( - has({ permission: "org:feature:inventory_add" }) || // Clerk feature flag - (await getUserOrgIds(context, userId)).includes(TARGET_ORG_ID) + isAuthenticated && + userId && + ( + !!has({ permission: "org:feature:inventory_add" }) || // Clerk feature flag + (await getUserOrgIds(context, userId)).includes(TARGET_ORG_ID) + ) ); // Expose the flag to your Astro pages via locals - context.locals.canAddInventory = canAddInventory ?? false; + context.locals.canAddInventory = Boolean(canAddInventory); // ── Admin route guard (unchanged) ─────────────────────────────────────────── if (isAdminRoute(context.request)) { @@ -49,6 +61,8 @@ export const onRequest = clerkMiddleware(async (auth, context) => { return context.redirect("/"); } } + + return next(); }); // ── Helper: fetch all org IDs the current user belongs to ─────────────────── diff --git a/src/pages/partials/cards.astro b/src/pages/partials/cards.astro index 5975ca1..733d8e4 100644 --- a/src/pages/partials/cards.astro +++ b/src/pages/partials/cards.astro @@ -182,7 +182,7 @@ const facets = searchResults.results.slice(1).map((result: any) => { - {facets.map((facet) => ( + {facets.map((facet: any) => (
{facetNames(facet.field_name)}
{(facet.counts.length > 20) &&