Phase 1: inject X-Company-Context header with localStorage persistence#80
Open
TLemmAI wants to merge 1 commit intofleetbase:mainfrom
Open
Phase 1: inject X-Company-Context header with localStorage persistence#80TLemmAI wants to merge 1 commit intofleetbase:mainfrom
TLemmAI wants to merge 1 commit intofleetbase:mainfrom
Conversation
… 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
fetchservice so every API request carriesX-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— newactiveCompanyContextgetter/setter backed bywindow.localStorageunder keyfleetbase.activeCompanyContext. Also exportsACTIVE_COMPANY_CONTEXT_STORAGE_KEYconstant +clearActiveCompanyContext()helper.addon/services/fetch.js—getHeaders()now injectsX-Company-ContextfromcurrentUser.activeCompanyContextat request-assembly time (read fresh each call; reactive).addon/instance-initializers/validate-company-context.js— boot-time: reads stored UUID, callsGET /v1/companies/current-contextto 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
GET /v1/companies/current-contextendpoint +CompanyContextResolvermiddleware) to land first.Test plan
pnpm test --filter "X-Company-Context"→ 3 unit tests passlocalStorage.setItem('fleetbase.activeCompanyContext', '<valid uuid>')then reload → Network panel shows header; set invalid UUID →/v1/companies/current-contextreturns 403 and localStorage is cleared on next boot.