Connect Google Calendar from the settings page
React · React · intermediate · modification
Bookings can finally go to the host's own calendar: the Calendar card on the Integrations settings page stops saying "coming soon" and gets a connect button. `src/settings/useCalendarConnect.ts` (new) opens the existing Google connect flow in a pop-up and listens for the result the flow's last page posts back. On `calendar-connected` it closes the pop-up and calls `PUT /api/me/calendar` with the `connectionId`; on `calendar-failed` it shows that consent was not given. It polls the pop-up so a closed window resets the card, ignores clicks while a connect is in progress, and removes its listener and timer when done or unmounted. `src/settings/CalendarCard.tsx` renders the button, the linked calendar and one status line. `src/settings/CalendarCard.test.tsx` (new) covers the card's states with a mocked `window.open`.
Booking app for consultants (hosts book meetings with their clients): a React 18.3 + TypeScript SPA (Vite, Vitest 2 with Testing Library 16 and jsdom) served only from `https://app.example.com`, which also serves the API under `/api`. The API authenticates with a `SameSite=Lax` session cookie and requires an `X-Requested-With: fetch` header on every write; `apiPut(path, body)` in `src/lib/api.ts` (existing, unchanged) sends JSON with both and resolves the parsed body on 2xx, else throws `ApiError`. Every page is served with `Content-Security-Policy: frame-ancestors 'none'` (it cannot be framed) and no `Cross-Origin-Opener-Policy` header, so a page on another site may open the settings page with `window.open` and keep a handle to that window, as browsers allow. The connect flow is existing and unchanged: `/oauth/calendar/start` redirects to Google's consent pages, Google returns to our callback, and the callback serves `https://app.example.com/oauth/calendar/done`. That page runs `window.opener.postMessage({ type: 'calendar-connected', connectionId }, 'https://app.example.com')` on success or `window.opener.postMessage({ type: 'calendar-failed' }, 'https://app.example.com')` when consent is declined, then shows "You can close this window". The callback rejects a `state` that is not bound to a cookie `/oauth/calendar/start` set in the same browser, and `/oauth/calendar/done` is only rendered by the callback, with the id written inline (never read from the URL). The server-rendered sign-up page (`/signup/calendar`, unchanged) runs the same flow before an account exists, so `/oauth/calendar/start` needs no sign-in and the callback stores each authorised Google account as a connection that belongs to no one yet: `connectionId` is a random 128-bit id, valid for 10 minutes and for one claim. `PUT /api/me/calendar` (existing) with `{ connectionId }` links that unclaimed connection to the signed-in user, replacing any linked calendar, and answers `{ email }` of the Google account, or 404 for an unknown, claimed or expired id. Once linked, the app writes each new booking of the user (title, time, client name and email) into that Google calendar. `initial` comes from `GET /api/me/calendar` when the settings page loads; the global stylesheet provides `settings-card`, `settings-note` and `button`.
Requirements
- The Calendar card on the Integrations settings page gets a button, "Connect Google Calendar" ("Connect a different calendar" when one is already linked). It opens the existing connect flow in a pop-up window (`/oauth/calendar/start`, 520 by 640). When the flow reports success, the page links the reported connection to the signed-in account with `PUT /api/me/calendar` and the card shows the linked calendar's address; linking replaces any calendar linked before.
- Security contract: the account is only ever linked to a connection that the signed-in user just authorised in the pop-up this page opened. Nothing that another window or another site sends to the settings page may link a calendar to the account.
- States, shown in the card's one `role="status"` line: while the pop-up is open the button is disabled and the line says "Finish connecting in the pop-up window."; while saving it says "Saving…"; on success "Calendar connected.". A blocked pop-up, a declined consent and a failed save each get their own message and leave the button enabled for a retry. Closing the pop-up before finishing returns the card to its idle state with no message. A second click while a connect is in progress does nothing.
- The page listens for the pop-up's result only while a connect is in progress, and stops when it gets the result, when the pop-up is closed, or when the card unmounts. Tests run with the existing `npm test`.
Files touched
- src/settings/useCalendarConnect.ts
- src/settings/CalendarCard.tsx
- src/settings/CalendarCard.test.tsx