Home Page and Customer Reset when logging out. #21

Closed
opened 2026-08-15 18:27:49 -05:00 by bermudalamb · 1 comment
Owner

As a customer, when I log off I should be redirected to the home page, and the Log In and Sign Up should be reset.

As a customer, when I log off I should be redirected to the home page, and the `Log In` and `Sign Up` should be reset.
bermudalamb self-assigned this 2026-08-15 18:38:14 -05:00
bermudalamb added this to the Initial Build project 2026-08-15 18:48:51 -05:00
bermudalamb moved this to In Progress in Initial Build on 2026-08-15 18:51:41 -05:00
bermudalamb moved this to Ready for Review in Initial Build on 2026-08-17 08:39:37 -05:00
Author
Owner

Fixed

Branch fix/logout-customer-reset, one commit, not yet pushed.

Cause

handleLogout destroyed the server session and navigated home, but never told CustomerAuthContext:

async function handleLogout() {
  await logoutCustomer();
  navigate('/');
}

customer stayed in React state, so the header kept rendering "My Account" instead of "Log in" and "Sign up". A page reload appeared to fix it — fetchMe() then returns null — which is why the symptom would have looked intermittent rather than consistently broken.

The cart badge had the same root cause: CartContext only clears its items once customer becomes null, so the old count stayed on screen too.

A second defect found alongside it

logoutCustomer ignored res.ok:

export function logoutCustomer(): Promise<void> {
  return fetch('/api/customers/logout', { method: 'POST' }).then(() => undefined);
}

A failed logout leaves the session cookie valid, so the UI would report success while the customer was still signed in — and the next reload would silently sign them back in. Same class as the false "Item added" in #23.

Changes

Change Why
logout() moved onto CustomerAuthContext, clearing customer directly The header only updates if the context knows. Clearing directly rather than calling refresh() avoids a window where the session is gone but the UI still shows the customer signed in
logoutCustomer rejects on a non-OK response A failed logout must not look like a successful one. Not routed through the existing handle() helper because the endpoint answers 204 with no body, which handle() would fail to parse
Account page reports the failure and stays put Better than navigating home while still authenticated
navigate('/', { replace: true }) Back no longer returns to the account page, which would only bounce to /login now the session is gone
Cart badge — no change needed Clears itself through the existing effect once customer is null

Tests

Four e2e tests added to auth.spec.ts, all written before the fix and confirmed failing first:

  • Logging out lands on / with "Log in" and "Sign up" visible and "My Account" gone
  • The logged-out header survives a reload — proves the session was really destroyed rather than the header merely repainted from stale client state
  • The account page is not left on the back stack
  • A failed logout reports the error and stays on the account page

Full suite: 47 backend unit, 33 e2e, stable over two consecutive runs. tsc --noEmit and npm run build clean.

Not changed

Items already reserved in a cart stay reserved until the normal expiry sweep — logging out does not release them. That is existing behaviour and outside what this issue asks for; worth its own issue if it should change.

## Fixed Branch `fix/logout-customer-reset`, one commit, not yet pushed. ### Cause `handleLogout` destroyed the server session and navigated home, but never told `CustomerAuthContext`: ```ts async function handleLogout() { await logoutCustomer(); navigate('/'); } ``` `customer` stayed in React state, so the header kept rendering "My Account" instead of "Log in" and "Sign up". A page reload appeared to fix it — `fetchMe()` then returns `null` — which is why the symptom would have looked intermittent rather than consistently broken. The cart badge had the same root cause: `CartContext` only clears its items once `customer` becomes `null`, so the old count stayed on screen too. ### A second defect found alongside it `logoutCustomer` ignored `res.ok`: ```ts export function logoutCustomer(): Promise<void> { return fetch('/api/customers/logout', { method: 'POST' }).then(() => undefined); } ``` A failed logout leaves the session cookie valid, so the UI would report success while the customer was still signed in — and the next reload would silently sign them back in. Same class as the false "Item added" in #23. ### Changes | Change | Why | | --- | --- | | `logout()` moved onto `CustomerAuthContext`, clearing `customer` directly | The header only updates if the context knows. Clearing directly rather than calling `refresh()` avoids a window where the session is gone but the UI still shows the customer signed in | | `logoutCustomer` rejects on a non-OK response | A failed logout must not look like a successful one. Not routed through the existing `handle()` helper because the endpoint answers `204` with no body, which `handle()` would fail to parse | | Account page reports the failure and stays put | Better than navigating home while still authenticated | | `navigate('/', { replace: true })` | Back no longer returns to the account page, which would only bounce to `/login` now the session is gone | | Cart badge — no change needed | Clears itself through the existing effect once `customer` is `null` | ### Tests Four e2e tests added to `auth.spec.ts`, all written before the fix and confirmed failing first: - Logging out lands on `/` with "Log in" and "Sign up" visible and "My Account" gone - The logged-out header survives a reload — proves the session was really destroyed rather than the header merely repainted from stale client state - The account page is not left on the back stack - A failed logout reports the error and stays on the account page Full suite: 47 backend unit, 33 e2e, stable over two consecutive runs. `tsc --noEmit` and `npm run build` clean. ### Not changed Items already reserved in a cart stay reserved until the normal expiry sweep — logging out does not release them. That is existing behaviour and outside what this issue asks for; worth its own issue if it should change.
bermudalamb moved this to Send back in Initial Build on 2026-08-17 14:00:51 -05:00
bermudalamb moved this to In Progress in Initial Build on 2026-08-17 14:00:56 -05:00
bermudalamb moved this to Ready for Review in Initial Build on 2026-08-17 14:01:01 -05:00
bermudalamb moved this to Review in Initial Build on 2026-08-17 15:24:03 -05:00
bermudalamb moved this to Ready for Release in Initial Build on 2026-08-17 15:24:06 -05:00
bermudalamb moved this to Released in Initial Build on 2026-08-17 15:24:10 -05:00
Sign in to join this conversation.
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: bermudalamb/redefined-designs#21