Core Concepts

Multi Tenant Session Scoping

A shared browser, such as a family computer, a work kiosk, or a laptop passed between roommates, can have two different people log into the same app minutes apart. Without an explicit boundary, one person's cached dashboard, queued edits, or in progress uploads can still be sitting there when the next person logs in. Scoping is how Flux draws that boundary, and keeps it intact even when a logout never happens cleanly. Encryption at rest makes that boundary hold on disk as well.

Why logging out is not enough on its own

A clean logout button is easy to build and easy to rely on, until a tab crashes, a laptop lid closes mid session, or an auth cookie silently expires with no logout event ever firing. Any of those leaves cached data belonging to person A still sitting in the browser when person B opens the same app. A caching layer that only clears itself when explicitly told to has a gap exactly where it matters most.

Flux closes that gap with independent safeguards rather than one: structural isolation, an explicit teardown call, a self healing check at the next session start, and encryption at rest, so that even if one of them is skipped entirely, the others still catch it.

1

Every store declares who it belongs to

Every registered channel carries an explicit scope, either 'global' (the default) for data that is the same for everyone and safe to keep indefinitely, or 'user' for data that belongs to one signed in person specifically: a dashboard, a billing panel, a private queue of edits. A 'user' scoped channel is physically kept apart from every other user's copy of that same channel, so one account's data is never reachable under another account's identity, even by accident.
lib/flux.ts (excerpt)
1// A public store, the default. Survives logout, shared by every visitor.
2flux.register({
3 channel: 'pricing',
4 idbKey: 'pricing:latest',
5 ttl: 'long',
6 event: 'UPDATE',
7 store: pricingStoreAdapter,
8})
9
10// A private store, namespaced per user, purged on logout.
11flux.register({
12 channel: 'account_dashboard',
13 idbKey: 'account_dashboard:latest',
14 ttl: 'short',
15 scope: 'user',
16 event: 'UPDATE',
17 store: dashboardStoreAdapter,
18})
2

A single call tears down everything private

Calling flux.clearUserSession() from your logout handler resets every 'user' scoped store back to empty, cancels any of that user's writes or fetches still in flight, stops tracking their in progress jobs, clears any conflict prompts waiting on their decision, and clears their cached data, all in one call. Public, 'global' scoped data is left completely untouched, since it never belonged to any one person in the first place.
What happens to the user's queued offline writes depends on whether encryption is configured. Without it, the queued mutations are dropped with the rest of the session. With encryption on (Step 4), they are kept sealed on disk and replay at that user's next sign in, so signing out never silently loses unsent work.
lib/auth.ts (excerpt)
1async function handleLogout() {
2 await flux.clearUserSession();
3 await supabase.auth.signOut();
4 router.push('/login');
5}
Every open tab hears about it

Logging out in one tab clears the session in every other tab open to the same app on that browser, not just the tab where the button was clicked. A second tab left open in the background never keeps holding a signed out user's data in memory.

3

A self healing check for the logout that never happened

Not every session ends with a logout call. A crashed tab, a closed laptop lid, or a cookie that silently expired all skip it entirely. To cover this, Flux checks the identity of whoever's session is starting up against whoever's session was there last. If they do not match, the previous person's private data is swept before the new session's data is ever loaded, with no explicit logout required to have triggered it.
This check is deliberately conservative about the reverse case. A public page loading before your app has confirmed whether anyone is logged in at all is the normal, expected order of events on every page load, not a sign that someone logged out. Flux only treats an anonymous state as confirmed once your app has explicitly said so, so a normal page load is never mistaken for a logout and never wipes a still valid session by mistake.
4

Encryption at rest for user scoped data

Isolation decides who is allowed to see a record. Encryption decides what a record looks like to anyone who reads the disk directly. Add an encryption block to createFlux() and every 'user' scoped cache entry and every 'user' scoped queued write is sealed with AES GCM before it reaches IndexedDB. The key is derived per user from a secret your own backend issues, so it never lives in your code, and an unlocked key stays on the device only for as long as the idle window allows.
lib/flux.ts (excerpt)
1// lib/flux.ts (excerpt)
2import { createFlux, createHttpKeyProvider } from '@tsworldtech/flux'
3
4export const flux = createFlux({
5 adapter,
6 storagePrefix: 'my_app_cache',
7
8 // Seals 'user' scoped cache entries and 'user' scoped queued writes
9 // with AES GCM before they reach IndexedDB. 'global' scoped data is
10 // never encrypted, it carries a checksum only.
11 encryption: {
12 keyProvider: createHttpKeyProvider({ endpoint: '/api/flux/key' }),
13 },
14})
DataScopeWhat is on disk
Cache entries'user'Ciphertext, bound to its key, timestamp and TTL
Cache entries'global'Readable data plus a checksum. Detects edits, not a security boundary.
Queued writes'user'Readable header with the user id, encrypted body
Queued writes'global'Readable body plus a checksum, userId is null
Scope now decides two things

The same scope field that namespaces a channel also decides whether its data is encrypted and which queue it belongs to. That is why a queued write needs both 'user' and a real user id: with either missing, Flux stores it as a global record that a signed in user's replay never picks up. See Steps 17 to 20 of the Next.js and React getting started guides for the full key route and queue setup.

The first unlock on a device needs the network

The first time a user unlocks on a device, Flux must reach your key route. After that the unlocked key is kept on the device, so offline cold starts keep working. A device that signs in for the first time while offline stays locked, and queued user writes are refused with FluxLockedError rather than stored as plaintext.

5

Two ways to sign out, and what happens to tampered data

With encryption on, there are two honest answers to "what happens to this user's unsent changes when they sign out?" A normal sign out keeps them sealed on disk for the next sign in. A sign out on a public computer discards them and destroys the user's key. Offer both choices in your sign out UI.
lib/auth.ts (excerpt)
1// Normal sign out: the cache is cleared, unsent writes stay sealed on
2// disk and replay at this user's next sign in.
3async function handleLogout() {
4 await flux.clearUserSession();
5 await supabase.auth.signOut();
6 router.push('/login');
7}
8
9// Public computer sign out: also wipes the queued writes and destroys
10// the user's key. Call it BEFORE your auth sign out.
11async function handleLogoutAndClearDevice() {
12 await flux.clearUserSession({ discardQueue: true });
13 await supabase.auth.signOut();
14 router.push('/login');
15}
SituationWhat Flux does
Signed out user's sealed writesStay on disk and replay at that user's next sign in.
Sealed writes of a user who never returnsDeleted after queueRetentionMs (default 30 days).
Another user signs in on the same deviceCannot read the first user's sealed writes and never replays them.
Stored record edited outside the appFails verification and is removed. Reported to your integrity listener.
components/FluxIntegrityModal.tsx (excerpt)
1// Fires for every stored record that fails verification, from any tab.
2// It calls back immediately with anything not yet acknowledged, so the
3// notice still appears after a reload.
4const unsubscribe = flux.subscribeToIntegrity((events) => {
5 showEditsDetectedNotice(events);
6});
7
8// Call it once the user has seen the notice.
9flux.acknowledgeIntegrity();
scope is optional and defaults to 'global', so registrations written before this existed keep working unchanged. In development, Flux will point out, with a console warning only and never a blocking error, if a channel name that sounds private (dashboard, billing, account, and similar) was left on the public default, so a private looking store does not quietly go unscoped by accident.
Available on every tier

Multi tenant scoping is not a paid feature. Every tier, including Free, gets the same isolation and cleanup guarantees. An app with authenticated dashboards is expected to need this correctly, regardless of what it is paying for. It is treated as a baseline correctness guarantee, not something to upsell.

For the field itself, see api/register and api/clear_user_session. For how scoping interacts with the offline queue and job tracker specifically, see core_concepts/offline_queue and core_concepts/job_tracker. For the key route and its environment variables, see guides/encryption_key_route.

Powered by Flux