fix(portal-shell): send withCredentials on /me so the session cookie crosses SPA→BFF in dev #114

Merged
julien merged 1 commits from fix/portal-shell/auth-me-with-credentials into main 2026-05-12 22:35:12 +02:00
Owner

Summary

Manual smoke after PR #113 surfaced a dev-only bug: after /auth/callback the BFF correctly sets the portal_session cookie and redirects to the SPA, but the SPA's next call to /api/auth/me comes back 401 with no cookie: header at all. The user lands "back at the portal" but the header still shows "Sign in".

Root cause. Angular's HttpClient via withFetch() inherits fetch's default credentials: 'same-origin'. In dev, localhost:4200 (SPA) → localhost:3000 (BFF) is cross-origin (different ports), so the browser drops the session cookie on the way out. SameSite=Lax is a red herring: both URLs share the registrable domain, so the cookie is still same-site — what was missing was opting the fetch into credentials.

Fix. Per-call withCredentials: true on the /me request. Only /me needs cookies today; login/logout are full-page navigations through window.location, which the browser hydrates with cookies regardless. A global HttpInterceptor will be the right abstraction once other authenticated BFF endpoints exist — premature for one consumer.

BFF side was already correct. enableCors({ credentials: true }) in main.ts. Nothing to change.

A new spec pins withCredentials === true on the /me request so a future refactor can't silently drop the flag and reintroduce the bug.

Test plan

  • pnpm nx test feature-auth9/9 pass (was 8 before; +1 spec pinning the credentials flag).
  • pnpm nx test portal-shell32/32 pass.
  • pnpm nx lint feature-auth portal-shell → clean.
  • pnpm nx build portal-shell → clean.
  • Manual smoke against the running BFF: anonymous landing → click "Sign in" → Entra → callback → SPA lands with avatar + display name in the header (the very last step that failed before this fix).
## Summary Manual smoke after PR #113 surfaced a dev-only bug: after `/auth/callback` the BFF correctly sets the `portal_session` cookie and redirects to the SPA, but the SPA's next call to `/api/auth/me` comes back **401 with no `cookie:` header at all**. The user lands "back at the portal" but the header still shows "Sign in". **Root cause.** Angular's `HttpClient` via `withFetch()` inherits `fetch`'s default `credentials: 'same-origin'`. In dev, `localhost:4200` (SPA) → `localhost:3000` (BFF) is cross-origin (different ports), so the browser drops the session cookie on the way out. SameSite=Lax is a red herring: both URLs share the registrable domain, so the cookie is still same-site — what was missing was opting the fetch into credentials. **Fix.** Per-call `withCredentials: true` on the /me request. Only /me needs cookies today; login/logout are full-page navigations through `window.location`, which the browser hydrates with cookies regardless. A global `HttpInterceptor` will be the right abstraction once other authenticated BFF endpoints exist — premature for one consumer. **BFF side was already correct.** `enableCors({ credentials: true })` in `main.ts`. Nothing to change. A new spec pins `withCredentials === true` on the /me request so a future refactor can't silently drop the flag and reintroduce the bug. ## Test plan - [x] `pnpm nx test feature-auth` → **9/9 pass** (was 8 before; +1 spec pinning the credentials flag). - [x] `pnpm nx test portal-shell` → **32/32 pass**. - [x] `pnpm nx lint feature-auth portal-shell` → clean. - [x] `pnpm nx build portal-shell` → clean. - [ ] Manual smoke against the running BFF: anonymous landing → click "Sign in" → Entra → callback → SPA lands with avatar + display name in the header (the very last step that failed before this fix).
julien added 1 commit 2026-05-12 22:34:52 +02:00
fix(portal-shell): send withCredentials on /me so the session cookie crosses SPA→BFF in dev
CI / commits (pull_request) Successful in 1m28s
CI / scan (pull_request) Successful in 1m40s
CI / check (pull_request) Successful in 1m56s
CI / a11y (pull_request) Successful in 1m40s
CI / perf (pull_request) Successful in 4m36s
dd375859a4
manual smoke surfaced the bug: the callback wrote the session and
set the portal_session cookie correctly, but the spa's next /me call
came back 401 with no cookie header at all.

root cause: angular's HttpClient via withFetch() inherits fetch's
default `credentials: 'same-origin'`. localhost:4200 → localhost:3000
is cross-origin (different ports), so the session cookie is dropped
on the way out — the bff never sees it. samesite=lax was a red
herring: localhost:4200 and localhost:3000 share the registrable
domain, so the cookie is still same-site; what was missing was
opting the fetch into credentials.

fix is per-call: only /me needs cookies today. login/logout are
full-page navigations through window.location, which the browser
hydrates with cookies regardless. a global HttpInterceptor will land
when other authenticated bff endpoints exist — premature for one
consumer.

bff side was already wired: enableCors({ credentials: true }) in
main.ts. nothing to change there.

a spec pins withCredentials=true on the /me request so a future
refactor can't silently drop the flag and reintroduce the bug.
julien merged commit 0e4f0fc611 into main 2026-05-12 22:35:12 +02:00
julien deleted branch fix/portal-shell/auth-me-with-credentials 2026-05-12 22:35:13 +02:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: julien/apf_portal#114