Skip to content

Commit 6daa608

Browse files
committed
fix: clear local storage when refresh token in cookies expires
1 parent 8429f24 commit 6daa608

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

lib/api/apiClient.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { clientConfig } from '@/lib/config/clientConfig'
22
import { APIError, NetworkError } from '@/lib/utils'
33
import { isValidMongoObjectId } from '@/lib/validation'
4-
import { API_ENDPOINTS } from '@/constants'
4+
import { API_ENDPOINTS, STORAGE_KEYS } from '@/constants'
55

66
interface APIClientConfig {
77
baseURL: string
@@ -222,6 +222,7 @@ class APIClient {
222222
errorData
223223
)
224224
} else {
225+
this.clearStoredUser()
225226
if (typeof window !== 'undefined') {
226227
window.location.href = '/login'
227228
}
@@ -312,9 +313,11 @@ class APIClient {
312313
if (refreshResponse.ok) {
313314
return true
314315
} else {
316+
this.clearStoredUser()
315317
return false
316318
}
317319
} catch {
320+
this.clearStoredUser()
318321
return false
319322
} finally {
320323
this.isRefreshing = false
@@ -325,6 +328,19 @@ class APIClient {
325328
return await this.refreshPromise
326329
}
327330

331+
private clearStoredUser() {
332+
if (typeof window === 'undefined') {
333+
return
334+
}
335+
336+
try {
337+
localStorage.removeItem(STORAGE_KEYS.USER_ID)
338+
localStorage.removeItem(STORAGE_KEYS.USER_EMAIL)
339+
} catch (error) {
340+
console.warn('Failed to clear auth storage:', error)
341+
}
342+
}
343+
328344
private combineAbortSignals(signals: AbortSignal[]): AbortSignal {
329345
const controller = new AbortController()
330346

0 commit comments

Comments
 (0)