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
- The header's "Sign out" link is replaced by a "Switch user" button. It signs the current user out on the server (`POST /api/auth/sign-out`) and then shows the sign-in form in place, without reloading the page, so the next person at the desk can sign in straight away.
- Reception PCs are shared by receptionists and nurses. After "Switch user", nothing the previous user loaded (today's appointments, patient details, clinical notes) may be shown to, or be reachable by, whoever signs in next on that PC: the next user sees only what their own requests to the server return.
- Roles: nurses can read a patient's clinical notes; receptionists cannot (the notes endpoint answers them 403, and the notes panel then says "Clinical notes are visible to nurses only."). Both roles can see today's appointments and patient details.
- If the sign-out call fails, the user stays signed in, the header says so, and the button can be pressed again.
- Tests for the new button run with the existing `npm test`.
Files touched
- src/auth/AuthContext.jsx
- src/components/Header.jsx
- src/components/Header.test.jsx