Switch user at the front desk without reloading

React · React · intermediate · modification

Receptionists and nurses share the reception PCs and swap many times a day, and today every sign-out is a full page load through the server's `/auth/sign-out` redirect, which drops the app and makes the next person wait for it to boot again. This PR moves the swap into the app. `src/auth/AuthContext.jsx` adds `signOut()`, which calls `POST /api/auth/sign-out` and resets the signed-in user, so `App` shows the sign-in form. `src/components/Header.jsx` replaces the link with a "Switch user" button that calls it, disables itself while the call runs and shows an alert if it fails. `src/components/Header.test.jsx` (new) renders the app signed in, presses the button and checks that the server is called and the sign-in form appears, and that a failed call keeps the user signed in with an alert.

Front-desk web app of a clinic: a React 18.3 single-page app built with Vite 5, function components, `@tanstack/react-query` 5, tests with Vitest 2, @testing-library/react 16 and jsdom (`npm test`). Existing and unchanged: `src/main.jsx` renders `<QueryClientProvider client={queryClient}><AuthProvider><App /></AuthProvider></QueryClientProvider>` once per page load. `src/queryClient.js` creates that one `QueryClient` with `staleTime: 60_000` and a `retry` that never retries a 4xx answer; `gcTime` is left at the library default. `App` shows a loading line until `/api/me` answers, the sign-in form when `user` is `null`, and otherwise `Header` and `FrontDesk`. `FrontDesk` lists today's appointments (`useQuery` key `['appointments', 'today']`); clicking one opens `PatientPage` (key `['patients', id]`), which contains `NotesPanel` (key `['patients', id, 'notes']`, `GET /api/patients/:id/notes`). `NotesPanel` shows the 403 message, an error alert, a loading line or the notes, in that order of checks. `src/api.js` sends same-origin `fetch` requests and throws `ApiError` with the status for any non-2xx answer. Sessions: the server sets an `HttpOnly; Secure; SameSite=Strict` session cookie on sign-in; the app never sees a token, and only keeps the signed-in user (`id`, `name`, `role`) in React state. `POST /api/auth/sign-out` ends the session server-side and expires the cookie, and answers 204 also when there is no valid session. The API checks the session and the role on every request and answers every `/api/*` request with `Cache-Control: no-store`. The app has no form drafts or other local storage; `localStorage`, `sessionStorage` and service workers are not used.

Requirements

Files touched

Review this PR

React practice