Merge pull request #15 from whtvrboo/fix/offline-logout-on-startup
Fix: Prevent automatic logout when starting app offline
This commit is contained in:
commit
7ef225daec
@ -57,6 +57,7 @@ export const useAuthStore = defineStore('auth', () => {
|
|||||||
|
|
||||||
const fetchCurrentUser = async () => {
|
const fetchCurrentUser = async () => {
|
||||||
if (!accessToken.value) {
|
if (!accessToken.value) {
|
||||||
|
// No token, so definitely clear any residual state and return.
|
||||||
clearTokens()
|
clearTokens()
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
@ -65,7 +66,28 @@ export const useAuthStore = defineStore('auth', () => {
|
|||||||
setUser(response.data)
|
setUser(response.data)
|
||||||
return response.data
|
return response.data
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
// 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()
|
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
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user