Skip to content

Phase 1: inject X-Company-Context header with localStorage persistence#80

Open
TLemmAI wants to merge 1 commit intofleetbase:mainfrom
TLemmAI:feat/multi-tenant-hierarchy
Open

Phase 1: inject X-Company-Context header with localStorage persistence#80
TLemmAI wants to merge 1 commit intofleetbase:mainfrom
TLemmAI:feat/multi-tenant-hierarchy

Conversation

@TLemmAI
Copy link
Copy Markdown

@TLemmAI TLemmAI commented Apr 15, 2026

Summary

Part of Phase 1 multi-tenant hierarchy work for the fleetbase TMS.

Adds client-side active-company-context state and wires it into the shared fetch service so every API request carries X-Company-Context: <uuid> when a context is set. Includes boot-time validation that clears the stored UUID if the backend rejects it (401/403).

What changes

  • addon/services/current-user.js — new activeCompanyContext getter/setter backed by window.localStorage under key fleetbase.activeCompanyContext. Also exports ACTIVE_COMPANY_CONTEXT_STORAGE_KEY constant + clearActiveCompanyContext() helper.
  • addon/services/fetch.jsgetHeaders() now injects X-Company-Context from currentUser.activeCompanyContext at request-assembly time (read fresh each call; reactive).
  • addon/instance-initializers/validate-company-context.js — boot-time: reads stored UUID, calls GET /v1/companies/current-context to validate; clears localStorage on 401/403.
  • app/instance-initializers/validate-company-context.js — re-export shim.
  • tests/unit/services/fetch-company-context-test.js — unit coverage for header presence / absence / reactivity / localStorage persistence.

Dependencies

  • Requires core-api PR (GET /v1/companies/current-context endpoint + CompanyContextResolver middleware) to land first.

Test plan

  • QUnit: pnpm test --filter "X-Company-Context" → 3 unit tests pass
  • Manual: in console dev-build, localStorage.setItem('fleetbase.activeCompanyContext', '<valid uuid>') then reload → Network panel shows header; set invalid UUID → /v1/companies/current-context returns 403 and localStorage is cleared on next boot.

… persistence and boot validation

Task 14 of the Phase 1 multi-tenant hierarchy plan. First Ember-side change
wiring the active-company-context selection end-to-end through the HTTP layer.

Note: the plan document lists ember-ui as the target repo, but the shared
fetch + current-user services actually live in ember-core (ember-ui only
re-exports them). Implementing in ember-core is the only way to honor the
single-source-of-truth + reactive-header constraints.

Changes:
- current-user service: new @Tracked activeCompanyContext getter/setter,
  read-through from localStorage under the exported constant
  ACTIVE_COMPANY_CONTEXT_STORAGE_KEY, write-through on set, graceful
  degradation when storage is disabled.
- fetch service: getHeaders() reads currentUser.activeCompanyContext at
  request-assembly time (never cached) and injects X-Company-Context when
  non-null. Empty/null means no header so backend's Auth::getCompany()
  chain falls back to the user's default company.
- instance-initializer validate-company-context: on boot, if a UUID is
  persisted, mirror it into the tracked property, then call
  GET /v1/companies/current-context. Clears localStorage + tracked state
  on 401/403 (revoked access). Other errors leave state untouched so a
  transient blip doesn't clobber a valid selection.
- Unit test exercising: header injection when context set, omission when
  unset, reactivity across changes, and localStorage persistence.

Manual test plan (to run post-merge once the console app is up):
1. Log in fresh — no X-Company-Context on requests (localStorage empty).
2. DevTools: localStorage.setItem('fleetbase.activeCompanyContext', '<uuid>')
   then reload. Requests should carry X-Company-Context: <uuid>.
3. Set an invalid UUID and reload. /v1/companies/current-context returns
   403; localStorage is cleared; subsequent requests omit the header.

Not included (deferred to Task 15):
- No switcher UI; setter is exposed but only called programmatically for now.
- No touching of routes, components, templates, or auth flow.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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