fix(frontend): prevent PWA logout on background resume#537
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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):
Refresh tokens stay single-use; token TTLs and backend config untouched.