Skip to content

fix(frontend): prevent PWA logout on background resume#537

Merged
algirdasc merged 3 commits into
mainfrom
fix/pwa-background-logout
Jul 18, 2026
Merged

fix(frontend): prevent PWA logout on background resume#537
algirdasc merged 3 commits into
mainfrom
fix/pwa-background-logout

Conversation

@algirdasc

Copy link
Copy Markdown
Owner

Mobile PWA logged users out after the app sat in the background past the 10-minute access token TTL. On resume, multiple API requests each detected the expired token and fired their own refresh. Because the backend issues single-use refresh tokens, only the first refresh succeeded; the rest got 401s, which UnauthorizedInterceptor caught indiscriminately - clearing tokens and redirecting to login even though a valid token had just been obtained.

Changes (frontend only, backend security unchanged):

  • Add TokenRefreshService that serializes concurrent refresh calls via shareReplay so the single-use refresh token is only ever sent once.
  • Add AuthJWTInterceptor (replaces NbAuthJWTInterceptor) routing the authenticate/refresh check through TokenRefreshService.
  • UnauthorizedInterceptor no longer clears tokens on 401s from the refresh endpoint (handled by the auth layer).
  • ErrorInterceptor no longer shows a toast for 401 responses.
  • Route guard uses the serialized refresh path.
  • Add AppResumeService to proactively refresh on visibilitychange, before the first API call fires on resume.

Refresh tokens stay single-use; token TTLs and backend config untouched.

Mobile PWA logged users out after the app sat in the background past the
10-minute access token TTL. On resume, multiple API requests each detected
the expired token and fired their own refresh. Because the backend issues
single-use refresh tokens, only the first refresh succeeded; the rest got
401s, which UnauthorizedInterceptor caught indiscriminately - clearing
tokens and redirecting to login even though a valid token had just been
obtained.

Changes (frontend only, backend security unchanged):
- Add TokenRefreshService that serializes concurrent refresh calls via
  shareReplay so the single-use refresh token is only ever sent once.
- Add AuthJWTInterceptor (replaces NbAuthJWTInterceptor) routing the
  authenticate/refresh check through TokenRefreshService.
- UnauthorizedInterceptor no longer clears tokens on 401s from the
  refresh endpoint (handled by the auth layer).
- ErrorInterceptor no longer shows a toast for 401 responses.
- Route guard uses the serialized refresh path.
- Add AppResumeService to proactively refresh on visibilitychange,
  before the first API call fires on resume.

Refresh tokens stay single-use; token TTLs and backend config untouched.
Mobile PWA logged users out after the app sat in the background past the
10-minute access token TTL. On resume, multiple API requests each detected
the expired token and fired their own refresh. Because the backend issues
single-use refresh tokens, only the first refresh succeeded; the rest got
401s, which UnauthorizedInterceptor caught indiscriminately - clearing
tokens and redirecting to login even though a valid token had just been
obtained.

Changes (frontend only, backend security unchanged):
- Add TokenRefreshService that serializes concurrent refresh calls via
  shareReplay so the single-use refresh token is only ever sent once.
- Add AuthJWTInterceptor (replaces NbAuthJWTInterceptor) routing the
  authenticate/refresh check through TokenRefreshService.
- UnauthorizedInterceptor no longer clears tokens on 401s from the
  refresh endpoint (handled by the auth layer).
- ErrorInterceptor no longer shows a toast for 401 responses.
- Route guard uses the serialized refresh path.
- Add AppResumeService to proactively refresh on visibilitychange,
  before the first API call fires on resume.

Refresh tokens stay single-use; token TTLs and backend config untouched.
…fix/pwa-background-logout

# Conflicts:
#	frontend/src/app/interceptors/auth-jwt.interceptor.ts
#	frontend/src/app/services/token-refresh.service.ts
@algirdasc
algirdasc merged commit 4fca381 into main Jul 18, 2026
4 checks passed
@algirdasc
algirdasc deleted the fix/pwa-background-logout branch July 18, 2026 09:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant