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

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.
This commit is contained in:
Julien Gautier
2026-05-12 22:28:28 +02:00
parent 9a9faf9a31
commit dd375859a4
2 changed files with 24 additions and 1 deletions
+12 -1
View File
@@ -58,7 +58,18 @@ export class AuthService {
*/
async refresh(): Promise<void> {
try {
const user = await firstValueFrom(this.http.get<CurrentUser>(this.meUrl));
// `withCredentials: true` is mandatory: the SPA at
// http://localhost:4200 calls the BFF at http://localhost:3000 —
// different origins, so `fetch`'s default `credentials:
// 'same-origin'` would drop the `__Host-portal_session` cookie
// and /me would always answer 401. Production (single origin
// behind the same edge) doesn't need it but it's harmless there.
// CORS on the BFF side already allows credentials
// (`apps/portal-bff/src/main.ts` → `enableCors({ credentials:
// true })`).
const user = await firstValueFrom(
this.http.get<CurrentUser>(this.meUrl, { withCredentials: true }),
);
this._state.set({ kind: 'authenticated', user });
} catch (err) {
this._state.set(toErrorState(err));