Add conditional logging for development, improve token refresh logic, and remove outdated comments
- Wrap `console.log` calls in `if (process.env.NODE_ENV === 'development')` to prevent production logs in `forms/page.tsx`. - Refactor token refresh logic by relying solely on the `refreshPromise` to avoid race conditions. - Remove outdated `isRefreshing` flag, utilizing `refreshPromise` as the primary lock. - Update comments in `IMPLEMENTATION_PLAN.md` to reflect the current progress and eliminate redundant sections.
This commit is contained in:
@@ -19,8 +19,10 @@ import config from '@/config/app.config';
|
||||
/**
|
||||
* Token refresh state management (singleton pattern)
|
||||
* Prevents race conditions when multiple requests fail with 401 simultaneously
|
||||
*
|
||||
* The refreshPromise acts as both the lock and the shared promise.
|
||||
* If it exists, a refresh is in progress - all concurrent requests wait for the same promise.
|
||||
*/
|
||||
let isRefreshing = false;
|
||||
let refreshPromise: Promise<string> | null = null;
|
||||
|
||||
/**
|
||||
@@ -46,11 +48,12 @@ const getAuthStore = async () => {
|
||||
/* istanbul ignore next */
|
||||
async function refreshAccessToken(): Promise<string> {
|
||||
// Singleton pattern: reuse in-flight refresh request
|
||||
if (isRefreshing && refreshPromise) {
|
||||
// If a refresh is already in progress, return the existing promise
|
||||
if (refreshPromise) {
|
||||
return refreshPromise;
|
||||
}
|
||||
|
||||
isRefreshing = true;
|
||||
// Create and store the refresh promise immediately to prevent race conditions
|
||||
refreshPromise = (async () => {
|
||||
try {
|
||||
const authStore = await getAuthStore();
|
||||
@@ -107,7 +110,7 @@ async function refreshAccessToken(): Promise<string> {
|
||||
|
||||
throw error;
|
||||
} finally {
|
||||
isRefreshing = false;
|
||||
// Clear the promise so future 401s will trigger a new refresh
|
||||
refreshPromise = null;
|
||||
}
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user