This commit is contained in:
2026-08-13 19:23:48 +08:00
parent df1471dc30
commit 69a0093d99
2 changed files with 23 additions and 1 deletions
+20
View File
@@ -18,6 +18,26 @@ afterEach(() => {
})
describe('API error diagnostics', () => {
it('does not attach a stale access token to anonymous authentication requests', async () => {
localStorage.setItem('cc-admin-token', 'stale-large-token')
let authorization: unknown
http.defaults.adapter = async (config) => {
authorization = config.headers.Authorization
return {
status: 200,
statusText: 'OK',
headers: {},
config,
data: { code: '200', message: 'success', data: null },
}
}
await http.post('/auth/login', { username: 'operator', password: 'secret' })
expect(authorization).toBeUndefined()
localStorage.removeItem('cc-admin-token')
})
it('treats only body code 200 as success', () => {
expect(unwrapApiResponse({ code: '200', message: 'success', data: { id: '1' } }))
.toEqual({ id: '1' })
+3 -1
View File
@@ -59,7 +59,9 @@ export function registerAuthSessionExpiredHandler(handler: AuthSessionExpiredHan
http.interceptors.request.use((config) => {
const token = localStorage.getItem('cc-admin-token')
if (token) {
const isAnonymousAuthEndpoint = typeof config.url === 'string'
&& ['/auth/login', '/auth/refresh', '/auth/register'].includes(config.url)
if (token && !isAnonymousAuthEndpoint) {
config.headers.Authorization = `Bearer ${token}`
}
return config