Skip to content

Commit feb2950

Browse files
committed
remove localstorage cache items on logout
1 parent 48f9d1a commit feb2950

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

frontend/src/services/auth/useAuth.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useContext } from 'react'
33
import { DialogContext } from '@/context/DialogContext.ts'
44
import { supabase } from '@/lib/supabase.ts'
55
import { authSSO, getCurrentUser, logout } from '@/services/auth/authService.ts'
6+
import { removeLocalCacheItems } from '@/services/collection/collectionService.ts'
67
import type { User } from '@/types'
78

89
export function useUser() {
@@ -14,6 +15,8 @@ export function useUser() {
1415
}
1516

1617
export function useLogout() {
18+
const { data: user } = useUser()
19+
const email = user?.user.email
1720
const queryClient = useQueryClient()
1821

1922
return useMutation({
@@ -24,6 +27,9 @@ export function useLogout() {
2427
await queryClient.invalidateQueries({ queryKey: ['account'] })
2528
await queryClient.invalidateQueries({ queryKey: ['collection'] })
2629
await queryClient.invalidateQueries({ queryKey: ['trade'] })
30+
if (email) {
31+
removeLocalCacheItems(email)
32+
}
2733
},
2834
})
2935
}

frontend/src/services/collection/collectionService.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ const COLLECTION_CACHE_KEY = 'tcg_collection_cache_v2'
55
const COLLECTION_TIMESTAMP_KEY = 'tcg_collection_timestamp_v2'
66
const PAGE_SIZE = 500
77

8+
export const removeLocalCacheItems = (email: string) => {
9+
// Invalidate local cache by removing records from localStorage
10+
localStorage.removeItem(`${COLLECTION_CACHE_KEY}_${email}`)
11+
localStorage.removeItem(`${COLLECTION_TIMESTAMP_KEY}_${email}`)
12+
}
13+
814
export const getCollection = async (email: string, collectionLastUpdatedRaw?: Date | string): Promise<Map<number, CollectionRow>> => {
915
if (!email) {
1016
throw new Error('Email is required to fetch collection')
@@ -112,9 +118,7 @@ export const updateCards = async (email: string, rowsToUpdate: CardAmountUpdate[
112118

113119
account = accountResult.data as AccountRow
114120
} catch (error) {
115-
// Invalidate local cache by removing records from localStorage
116-
localStorage.removeItem(`${COLLECTION_CACHE_KEY}_${email}`)
117-
localStorage.removeItem(`${COLLECTION_TIMESTAMP_KEY}_${email}`)
121+
removeLocalCacheItems(email)
118122
throw error
119123
}
120124

0 commit comments

Comments
 (0)