diff --git a/src/app/(dashboard)/presentations/page.tsx b/src/app/(dashboard)/presentations/page.tsx index a7f70a3..10c5427 100644 --- a/src/app/(dashboard)/presentations/page.tsx +++ b/src/app/(dashboard)/presentations/page.tsx @@ -25,7 +25,10 @@ export default async function PresentationsPage() { Query.limit(200), ], }), - ]); + ]).catch((e) => { + console.error("[PresentationsPage] data fetch failed:", e); + throw e; + }); const presentations = JSON.parse(JSON.stringify(presResult.rows)) as Presentation[]; diff --git a/src/lib/appwrite/active-context.ts b/src/lib/appwrite/active-context.ts index 063e4a3..b06926a 100644 --- a/src/lib/appwrite/active-context.ts +++ b/src/lib/appwrite/active-context.ts @@ -14,6 +14,18 @@ export type ActiveContext = { settings: TenantSettings | null; }; +async function setActiveTenantCookie(tenantId: string) { + try { + (await cookies()).set(ACTIVE_TENANT_COOKIE, tenantId, { + path: "/", + httpOnly: true, + sameSite: "strict", + secure: process.env.NODE_ENV === "production", + maxAge: 60 * 60 * 24 * 365, + }); + } catch { /* ignore in middleware context */ } +} + export async function getActiveContext(): Promise { const user = await getCurrentUser(); if (!user) return null; @@ -22,17 +34,19 @@ export async function getActiveContext(): Promise { let tenantId = await getActiveTenantId(); - // Validate cookie tenantId — user must actually be a member of that team. + // Validate cookie/prefs tenantId — team must exist and user must be a member. if (tenantId) { try { + await adminTeams.get(tenantId); // throws if team was deleted const memberships = await adminTeams.listMemberships(tenantId); const isMember = memberships.memberships.some((m) => m.userId === user.$id); if (!isMember) { - // Stale or cross-account cookie — clear and re-resolve. try { (await cookies()).delete(ACTIVE_TENANT_COOKIE); } catch { /* ignore */ } tenantId = null; } } catch { + // Team deleted or inaccessible — clear stale pointer. + try { (await cookies()).delete(ACTIVE_TENANT_COOKIE); } catch { /* ignore */ } tenantId = null; } } @@ -40,6 +54,8 @@ export async function getActiveContext(): Promise { if (!tenantId) { const userTeams = await getUserTeams(); tenantId = userTeams?.teams[0]?.$id ?? null; + // Persist so the next request skips this resolution path. + if (tenantId) await setActiveTenantCookie(tenantId); } if (!tenantId) return null;