English
Checkpoint
What must work
Run in Source/Chatex.Web:
powershell
npm run buildpowershell
npm test -- --watch=falsepowershell
npm run lintpowershell
npm run format:checkCompare the finished files with the reference:
powershell
git diff --no-index --stat Source/Chatex.Web/src/app/core/api D:\Work\Chatex-Reference\Source\Chatex.Web\src\app\core\apipowershell
git diff --no-index --stat Source/Chatex.Web/src/app/core/auth D:\Work\Chatex-Reference\Source\Chatex.Web\src\app\core\authpowershell
git diff --no-index Source/Chatex.Web/src/app/app.config.ts D:\Work\Chatex-Reference\Source\Chatex.Web\src\app\app.config.tsAll three print nothing. src/testing/fakes.ts still lacks the realtime fake of topic 20.
Commit:
powershell
git add .powershell
git commit -m "Add the API client, problem details, the session, the interceptor and the guards"Common mistakes
Table 16.3. Common mistakes in the API and auth layer
| Symptom | Cause | Fix |
|---|---|---|
Expected one matching request for criteria "Match URL: /api/auth/refresh", found 2 requests. | refresh() assigns with = instead of ??=, so every caller starts its own refresh | keep the promise of the running refresh and return it |
Expected no open requests, found 1: POST /api/auth/refresh | the code under test sent a request that the test did not expect | answer it with expectOne(...).flush(...), or fix the extra call |
NG0203: The `Router` token injection failed. `inject()` function must be called from an injection context … | inject() called inside a method or a callback | inject in a field initializer or the constructor and keep the reference |
'provideAppInitializer' is defined but never used from lint, and a signed-in user lands on /login after every reload | the initializer line was lost; the guards run before the session is restored | restore provideAppInitializer(() => inject(AuthService).restoreSession()) |
| every API call answers 401 although the sign-in worked | provideHttpClient() without withInterceptors([authInterceptor]): HttpClient works, but no token is added | register the interceptor in app.config.ts |
| the user is signed out on all devices now and then | two refresh requests raced and the server saw a reused refresh token | refresh only through the single-flight AuthService.refresh() |
Self-check questions
- Why are the DTOs described by interfaces and not by classes? What do these types check, and what not?
- Why are ids and dates strings in the client models?
- What is the difference between an Observable and a Promise? Where does Chatex use each?
- Which message does
problemMessagechoose for a validation error without a known code? For a network failure? - Why is the access token kept in memory? How does the session survive a page reload?
- What would go wrong without the single-flight refresh? Which server rule makes it necessary?
- Why does the interceptor compare the token it sent with the current token before refreshing?
- Why can the retried request not loop into another refresh?
- Why does a guard return a
UrlTreeinstead of navigating itself? - What does
provideAppInitializerguarantee, and what would the guards do without it?
Exercises
- A request id. Write a second interceptor that adds an
X-Request-Idheader withcrypto.randomUUID()to every/api/call, register it afterauthInterceptor, and write a test withHttpTestingController. - Offline message.
problemMessagetreats status 0 as a network failure. Add a test for anHttpErrorResponsewith status 503 and no body. Which sentence does the user get? Add a friendlier one for 503 and 429. - Proactive renewal. Use
vi.useFakeTimers()to test thatAuthServicerefreshes one minute before the token expires: sign in with a token valid for 15 minutes, advance the clock by 14 minutes and expect one refresh request.