Skip to content

fix(auth): demo cookie cleanup in middleware#92

Closed
NicholaiVogel wants to merge 3 commits intomainfrom
fix/demo-cookie-auth-priority
Closed

fix(auth): demo cookie cleanup in middleware#92
NicholaiVogel wants to merge 3 commits intomainfrom
fix/demo-cookie-auth-priority

Conversation

@NicholaiVogel
Copy link
Copy Markdown
Contributor

Summary

  • Move demo cookie deletion from getCurrentUser() (Server Component, where cookies().delete() silently fails) to middleware where response cookies actually work
  • Real WorkOS sessions now actively delete stale demo cookies via Set-Cookie on the response
  • /demo route clears compass-active-org so demo mode doesn't inherit a real user's workspace

Context

PR #91 reordered getCurrentUser() to check WorkOS before the demo cookie -- correct priority, but the cookie deletion was a no-op from Server Component context. This meant the demo cookie persisted for its full 24h lifetime, causing the middleware to short-circuit auth checks on every request even when a real session existed.

Test plan

  • Log in with real credentials, switch orgs, navigate -- stays in selected org
  • Visit /demo -- lands in demo org (Meridian Group), not previous real org
  • Log in again after demo -- demo cookie cleared by middleware, lands in real workspace
  • Verify compass-demo cookie absent after real login via browser devtools

Add org-scoped data isolation across all server actions to
prevent cross-org data leakage. Add read-only demo mode with
mutation guards on all write endpoints.

Multi-tenancy:
- org filter on executeDashboardQueries (all query types)
- org boundary checks on getChannel, joinChannel
- searchMentionableUsers derives org from session
- getConversationUsage scoped to user, not org-wide for admins
- organizations table, members, org switcher component

Demo mode:
- /demo route sets strict sameSite cookie
- isDemoUser guards on all mutation server actions
- demo banner, CTA dialog, and gate components
- seed script for demo org data

Also: exclude scripts/ from tsconfig (fixes build), add
multi-tenancy architecture documentation.
The demo cookie was checked unconditionally before WorkOS auth,
so logging in with real credentials after visiting /demo still
returned the demo user. Now getCurrentUser() tries WorkOS first
and only falls back to the demo cookie when no real session
exists. Clears the stale cookie on real login.
The previous fix reordered getCurrentUser() to prefer WorkOS
sessions, but cookie deletion silently failed because it ran
from a Server Component (cookies().delete() only works in
Server Actions and Route Handlers).

Move demo cookie cleanup to middleware where response cookies
work. Real sessions now actively delete the stale demo cookie.
Demo route also clears compass-active-org so demo mode doesn't
inherit a real user's workspace selection.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant