mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-21 15:06:41 +02:00
Projects included: - maerchenzauber (NestJS backend + Expo mobile + SvelteKit web + Astro landing) - manacore (Expo mobile + SvelteKit web + Astro landing) - manadeck (NestJS backend + Expo mobile + SvelteKit web) - memoro (Expo mobile + SvelteKit web + Astro landing) This commit preserves the current state before monorepo restructuring. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2.7 KiB
2.7 KiB
Refresh Token Grace Period Implementation
Overview
As of 2025-01-10, the backend implements a 5-minute grace period for refresh tokens to handle race conditions and network issues gracefully. This document explains how the frontend handles this feature.
How It Works
- Normal Flow: When refreshing a token, the old token is marked as "rotated" and remains valid for 5 minutes
- Grace Period: If the same old token is used again within 5 minutes, the backend returns the previously generated new token
- No Duplicates: This prevents creating multiple tokens for the same device
Frontend Implementation
TokenManager Updates
The tokenManager.ts already handles most scenarios correctly:
// Handles both refresh_in_progress and rotation_in_progress errors
if (result.error === 'refresh_in_progress' || result.error === 'rotation_in_progress') {
console.debug('TokenManager: Token rotation in progress, waiting...');
await new Promise(resolve => setTimeout(resolve, 1000));
// Retry after waiting
}
Non-Retryable Errors
The following errors indicate permanent failures and should not be retried:
invalid_token- Token doesn't existtoken_expired- Token or grace period expiredinvalid_token_state- Token in unexpected statetoken_collision- Very rare UUID collision
What the Frontend Does
- Always saves the returned token - Whether it's new or from grace period
- Retries with exponential backoff - For network and temporary errors
- Handles rotation_in_progress - Waits and retries when another request is rotating the token
- Respects permanent errors - Doesn't retry when token is truly invalid
Testing the Grace Period
To test the grace period implementation:
-
Simulate Network Interruption:
- Start a refresh request
- Kill the app before it completes
- Restart and try to refresh again
- Should succeed using grace period
-
Test Concurrent Refreshes:
- Make multiple refresh calls simultaneously
- All should succeed without creating duplicate tokens
-
Test Grace Period Expiry:
- Use an old token
- Wait 6+ minutes
- Try to use it again
- Should fail with "Token has been rotated and grace period has expired"
No Additional Changes Required
The current frontend implementation is already compatible with the grace period feature because:
- It properly retries on temporary errors
- It saves whatever token is returned
- It handles the new
rotation_in_progresserror - It respects permanent failure errors
The grace period is transparent to the frontend - it just makes the system more resilient to network issues and race conditions.