diff --git a/libs/feature/auth/src/lib/auth.service.spec.ts b/libs/feature/auth/src/lib/auth.service.spec.ts index 7aac605..83ebee3 100644 --- a/libs/feature/auth/src/lib/auth.service.spec.ts +++ b/libs/feature/auth/src/lib/auth.service.spec.ts @@ -62,6 +62,18 @@ describe('AuthService', () => { expect(service.isLoading()).toBe(false); }); + it('issues /me with withCredentials so the session cookie crosses the SPA→BFF origin gap', () => { + const { http } = setup(); + const req = http.expectOne(ME_URL); + // Without this, `fetch`'s default `credentials: 'same-origin'` + // would suppress the session cookie on the cross-origin + // (localhost:4200 → localhost:3000) request and /me would + // always answer 401 in dev. Verified manually against the BFF + // log on 2026-05-12. + expect(req.request.withCredentials).toBe(true); + req.flush({ error: 'unauthenticated' }, { status: 401, statusText: 'Unauthorized' }); + }); + it('transitions to anonymous on 401', async () => { const { service, http } = setup(); http diff --git a/libs/feature/auth/src/lib/auth.service.ts b/libs/feature/auth/src/lib/auth.service.ts index cc8043a..212fe0b 100644 --- a/libs/feature/auth/src/lib/auth.service.ts +++ b/libs/feature/auth/src/lib/auth.service.ts @@ -58,7 +58,18 @@ export class AuthService { */ async refresh(): Promise { try { - const user = await firstValueFrom(this.http.get(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(this.meUrl, { withCredentials: true }), + ); this._state.set({ kind: 'authenticated', user }); } catch (err) { this._state.set(toErrorState(err));