fix: update ApiManager to handle navigation on 401 Unauthorized responses and navigation for login

This commit is contained in:
Mo Tarbin
2025-05-07 00:21:36 -04:00
parent bce135cba8
commit 992d7e3303
2 changed files with 18 additions and 3 deletions

View File

@@ -11,6 +11,7 @@ class ApiManager {
constructor() {
this.customServerURL = `${API_URL}/api/v1`
this.initialized = false
this.navigateToLogin = () => {}
}
async init() {
@@ -35,6 +36,9 @@ class ApiManager {
this.customServerURL = url
this.init()
}
setNavigateToLogin(callback) {
this.navigateToLogin = callback
}
}
export const apiManager = new ApiManager()
@@ -68,6 +72,13 @@ export async function Fetch(url, options) {
const optionsHash = murmurhash.v3(JSON.stringify(options))
await localStore.saveToCache(fullURL + optionsHash, data)
networkManager.setOnline()
} else if (response.status === 401) {
// Handle 401 Unauthorized
const errorData = await response.json()
console.error('Unauthorized:', errorData)
localStorage.removeItem('ca_token')
localStorage.removeItem('ca_expiration')
apiManager.navigateToLogin()
} else if (
response.status === 503 ||
response.type === 'opaque' ||