English
Topic 16. The API client and authentication: typed models, HTTP services, a signal-based session, interceptor and guards
Goal: connect the client to the API: TypeScript models of the DTOs, thin HTTP services, Problem Details turned into readable messages, a session kept in signals with a single-flight token refresh, an interceptor that adds and renews tokens, route guards, and the tests that prove it.
Where we are
The lowest layer of the client, core/api and core/auth (Fig. 16.1). Everything the later topics show on screen goes through it.
Fig. 16.1. The API and authentication layer of the client
- Starting point: topic 15 — the workspace builds, 3 tests pass, and the dev server forwards
/apiand/hubsto the API. - At the end of the topic the client knows every endpoint of the API, restores a session from the refresh cookie at start-up, adds the bearer token to API calls and renews it transparently; 19 tests pass.
Steps
- Models and HTTP services — DTOs as TypeScript types · Observables · one service per resource
- Problem Details on the client — reading
application/problem+json· friendly messages · the first spec - The session — the token in memory · signals for the user · single-flight refresh · testing with
HttpTestingController - The interceptor and the guards — functional interceptor · retry after 401 · route guards · restoring the session at start-up
- Checkpoint — what must work, self-check questions and exercises
Materials
- Course slides, part 4, Real time and the TypeScript client (slides "The Angular client", "Single-flight refresh")
core/api/,core/auth/,app.config.ts,testing/fakes.tsDocs/Frontend.md– the session rules,Docs/HttpApi.md– the endpoints and error codes