diff --git a/fe/src/stores/auth.ts b/fe/src/stores/auth.ts index 1996562..7c6d386 100644 --- a/fe/src/stores/auth.ts +++ b/fe/src/stores/auth.ts @@ -57,6 +57,7 @@ export const useAuthStore = defineStore('auth', () => { const fetchCurrentUser = async () => { if (!accessToken.value) { + // No token, so definitely clear any residual state and return. clearTokens() return null } @@ -65,7 +66,28 @@ export const useAuthStore = defineStore('auth', () => { setUser(response.data) return response.data } catch (error: any) { - clearTokens() + // Check if the error is from an Axios request and has a response status + if (error.isAxiosError && error.response) { + const status = error.response.status + if (status === 401 || status === 403) { + // Authentication error from the server, clear tokens. + console.error('Authentication error fetching user, clearing tokens:', error) + clearTokens() + } else { + // Other HTTP error, log it but don't clear tokens. + // The user might be null, but the token remains for other cached calls. + console.error('HTTP error fetching user, token preserved:', error) + } + } else { + // Network error (offline) or other non-HTTP error. + // Log the error but preserve tokens. + // This allows the app to function with cached data if available. + console.error('Network or other error fetching user, token preserved:', error) + } + // In all error cases where tokens are not cleared, return null for the user object. + // The existing user object (if any) will remain until explicitly cleared or overwritten. + // If the intention is to clear the user object on any fetch error, uncomment the next line: + // setUser(null); return null } }