# Sign out and switch accounts (https://effetch-query.monarcode.dev/docs/cache/accounts)

A cache must not outlive the account that filled it: after a sign-out,
the next person at the screen shouldn't see the last one's todos. There
are two ways to drop it: make a client per account, or keep one client
and clear it. This page shows both, and when each is enough.

A session can end without your sign-out button. The user signs out in
another tab, a refresh token is refused, or a 401 signs them out. Another
account can sign in from another tab, and this tab follows it. Your
button's click handler runs in none of these, so nothing clears the cache:
the tab refetches the old account's queries with no session, or shows them
to the new account.

The examples import `api.ts` from [Getting started](https://effetch-query.monarcode.dev/docs/getting-started).

## Make a client per account

Give `QueryProvider` a `make` instead of a `client`, and the account's ID
as `resetKey`. It makes a client for each account, and disposes the one
before. It follows the account, not the click, so it handles every way a
session ends or changes:

```tsx
import { QueryClient } from "@monarcode/effetch-query";
import { QueryProvider } from "@monarcode/effetch-query/react";
import type { ReactNode } from "react";

import { ApiClientLive } from "./api.js";

// The signed-in account's ID, or "signed-out". It changes on every sign-in,
// sign-out and switch, in every tab.
declare function useAccountId(): string;

export function Providers({ children }: { readonly children: ReactNode }) {
  const accountId = useAccountId();
  return (
    <QueryProvider
      make={() => QueryClient.make({ layer: ApiClientLive })}
      resetKey={accountId}
    >
      {children}
    </QueryProvider>
  );
}
```

With `make` and `resetKey`:

- **Nothing of the old account shows.** When `resetKey` changes, the tree
  renders with the new client from that very render, and fetches afresh.
  The old client is disposed: its cache goes, and its fetches and mutation
  runs are interrupted.
- **There's one client per `resetKey`.** A new `make` function on each
  render doesn't make a new client, so an inline arrow is fine. If React
  renders without committing, as StrictMode's second render or an
  abandoned transition does, the extra client it made is disposed.
- **Put it above every `Suspense` boundary.** A first render that React
  throws away before showing it leaves its client undisposed.

With `client`, the provider never disposes the client: it's yours. A
provider can switch between the two forms, and the client it owned is
disposed when a `client` replaces it. The props are an
`OwnedQueryProviderProps` with `make`, and a `QueryProviderProps` with
`client`.

**Details: with server rendering**

If you render on the server, as
[Server rendering](https://effetch-query.monarcode.dev/docs/server-rendering) describes, two more rules
apply.

`make` is for the browser and React Native. Effects don't run in
server rendering, so a client made there is never disposed. On the
server, keep `client` with one client per request.

A `HydrationBoundary` inside doesn't carry state across accounts.
Into a new client, it loads only what the server dehydrated after the
change, comparing the server's dehydration time against the browser's
clock. What came before belongs to the previous account, so the new
client fetches it.

## Keep one client and clear it

If only your own sign-out button can end the session, clearing is
simpler. `client.clear()` drops every value without fetching, so the
screens still showing as you sign out don't refetch with no session and
fail with a 401:

```tsx
import { useQueryClient } from "@monarcode/effetch-query/react";

declare function endSession(): Promise<void>;

export function SignOut() {
  const queryClient = useQueryClient();
  return (
    <button
      onClick={async () => {
        await endSession();
        await queryClient.clear();
      }}
    >
      Sign out
    </button>
  );
}
```

From Effect, `Query.clear([])` does the same. After a clear:

- **Queries in use show `Initial`** until something asks for them: a
  component that starts using one, `client.fetch`, `invalidate` or
  `refetch`. A component already showing one keeps showing `Initial`, or
  its Suspense fallback. So after the next sign-in, navigate to a screen
  that mounts its queries, or call `queryClient.invalidate()`, so the
  screens ask again.
- **What depends on a cleared query goes too.** A query that reads a
  cleared one through `ctx.result` is cleared as well.
- **Nothing in progress brings it back.** A `client.fetch` waiting on a
  cleared query is interrupted, and a mutation run in progress doesn't
  roll its optimistic writes back over the cleared values.
- **Values from the server go too.** A query no one has read since the
  server's values were loaded into the client fetches the next time it's
  used.

`client.reset()` drops the values too, but fetches those in use again at
once.

> A mutation that settles after you clear can refetch with no session.
> `invalidate` asks for the queries it matches, so it fetches cleared ones
> again. If every mutation invalidates the whole cache
> (`mutations: { onSuccess: () => Query.invalidate([]) }`), one that
> settles after signing out fetches the queries in use with no session. A
> client per account doesn't: disposing it interrupts its mutation runs.

## What's next

- [Read and write the cache](https://effetch-query.monarcode.dev/docs/cache): `clear`, `reset` and the other
  cache functions, from Effect and from plain code.
- [Send the cache](https://effetch-query.monarcode.dev/docs/server-rendering): render on the server with a
  client per request.
- [Defaults and app-wide hooks](https://effetch-query.monarcode.dev/docs/client/defaults): run code after
  every query or mutation, for logging or error reporting.