mirror of
https://github.com/sasjs/adapter.git
synced 2026-04-21 13:11:31 +00:00
fix: clear all cookies on session expiry and throw LoginRequiredError
This commit is contained in:
@@ -84,7 +84,16 @@ export class RequestClient implements HttpClient {
|
|||||||
this.clearLocalStorageTokens()
|
this.clearLocalStorageTokens()
|
||||||
}
|
}
|
||||||
if (typeof document !== 'undefined') {
|
if (typeof document !== 'undefined') {
|
||||||
document.cookie = 'XSRF-TOKEN=; Max-Age=0; Path=/;'
|
this.clearAllCookies()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private clearAllCookies() {
|
||||||
|
const cookies = document.cookie.split(';')
|
||||||
|
for (const cookie of cookies) {
|
||||||
|
const name = cookie.split('=')[0].trim()
|
||||||
|
if (!name) continue
|
||||||
|
document.cookie = `${name}=; Max-Age=0; Path=/;`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -711,6 +720,10 @@ ${resHeaders}${parsedResBody ? `\n\n${parsedResBody}` : ''}
|
|||||||
this.isRecoveringFromNetworkError = true
|
this.isRecoveringFromNetworkError = true
|
||||||
try {
|
try {
|
||||||
return await callback()
|
return await callback()
|
||||||
|
} catch {
|
||||||
|
// Retry also failed — session is dead, surface LoginRequiredError
|
||||||
|
// so the app can prompt re-authentication.
|
||||||
|
throw new LoginRequiredError()
|
||||||
} finally {
|
} finally {
|
||||||
this.isRecoveringFromNetworkError = false
|
this.isRecoveringFromNetworkError = false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -607,7 +607,7 @@ ${resHeaders[0]}: ${resHeaders[1]}${
|
|||||||
expect(requestClient['csrfToken']).toEqual({ headerName: '', value: '' })
|
expect(requestClient['csrfToken']).toEqual({ headerName: '', value: '' })
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should not loop if retry also fails with ERR_NETWORK', async () => {
|
it('should throw LoginRequiredError if retry also fails with ERR_NETWORK', async () => {
|
||||||
const networkError = {
|
const networkError = {
|
||||||
isAxiosError: true,
|
isAxiosError: true,
|
||||||
code: 'ERR_NETWORK',
|
code: 'ERR_NETWORK',
|
||||||
@@ -621,7 +621,7 @@ ${resHeaders[0]}: ${resHeaders[1]}${
|
|||||||
|
|
||||||
await expect(
|
await expect(
|
||||||
requestClient['handleError'](networkError, innerHandle)
|
requestClient['handleError'](networkError, innerHandle)
|
||||||
).rejects.toEqual(networkError)
|
).rejects.toThrow(LoginRequiredError)
|
||||||
|
|
||||||
expect(innerHandle).toHaveBeenCalledTimes(1)
|
expect(innerHandle).toHaveBeenCalledTimes(1)
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user