fix: ai audit improvements
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest'
|
||||
import { setActivePinia, createPinia } from 'pinia'
|
||||
import { fetchWithAuth, apiClient } from '../client'
|
||||
import { fetchWithAuth, apiClient, SessionExpiredError } from '../client'
|
||||
import { useAuthStore } from '@/stores/auth'
|
||||
import * as authApi from '@/api/auth'
|
||||
import router from '@/router'
|
||||
@@ -122,7 +122,7 @@ describe('api client (fetchWithAuth)', () => {
|
||||
} as unknown as Response)
|
||||
global.fetch = fetchMock
|
||||
|
||||
await fetchWithAuth('/lists')
|
||||
await expect(fetchWithAuth('/lists')).rejects.toThrow(SessionExpiredError)
|
||||
|
||||
expect(fetchMock).toHaveBeenCalledTimes(1)
|
||||
expect(router.currentRoute.value.name).toBe('login')
|
||||
@@ -144,7 +144,7 @@ describe('api client (fetchWithAuth)', () => {
|
||||
} as unknown as Response)
|
||||
global.fetch = fetchMock
|
||||
|
||||
await fetchWithAuth('/lists')
|
||||
await expect(fetchWithAuth('/lists')).rejects.toThrow(SessionExpiredError)
|
||||
|
||||
expect(fetchMock).toHaveBeenCalledTimes(1)
|
||||
expect(authStore.accessToken).toBeNull()
|
||||
|
||||
+13
-2
@@ -6,6 +6,17 @@ export { API_BASE_URL, extractErrorMessage }
|
||||
|
||||
let refreshPromise: Promise<unknown> | null = null
|
||||
|
||||
// Thrown instead of returning the stale 401 Response when redirecting to
|
||||
// login, so callers show an accurate message rather than reading `!response.ok`
|
||||
// and flashing an unrelated "Failed to ..." banner in the instant before
|
||||
// navigation away completes.
|
||||
export class SessionExpiredError extends Error {
|
||||
constructor() {
|
||||
super('Your session has expired. Please log in again.')
|
||||
this.name = 'SessionExpiredError'
|
||||
}
|
||||
}
|
||||
|
||||
async function redirectToLogin(): Promise<void> {
|
||||
const currentRoute = router.currentRoute.value
|
||||
if (currentRoute.name === 'login') {
|
||||
@@ -57,7 +68,7 @@ export async function fetchWithAuth(
|
||||
if (response.status === 401 && !skipRefresh) {
|
||||
if (!authStore.refreshToken) {
|
||||
await redirectToLogin()
|
||||
return response
|
||||
throw new SessionExpiredError()
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -74,7 +85,7 @@ export async function fetchWithAuth(
|
||||
})
|
||||
} catch {
|
||||
await redirectToLogin()
|
||||
return response
|
||||
throw new SessionExpiredError()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user