Skip to content

Commit

Permalink
fix: auth interceptor reading status if undefined
Browse files Browse the repository at this point in the history
When an error occurred in an axios request before the request was even
sent, i.e. when a request interceptor causes an error, the
authInterceptor causes an error as it tries to read the property status.

Fixed authInterceptor to accept errors without response.
  • Loading branch information
Gido Manders committed Mar 24, 2023
1 parent ee2b224 commit f2a9be8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export function getXSRFToken(): string {
* Axios interceptor to automatically log the user out in the Redux store
* when the request you sent returns a 401 Not Authenticated.
*/
export function authInterceptor(error: { response: { status: number } }) {
if (error.response.status === 401) {
export function authInterceptor(error: { response?: { status: number } }) {
if (error.response?.status === 401) {
getService().logout();
}

Expand Down
17 changes: 17 additions & 0 deletions tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@ describe('authInterceptor', () => {

expect(logoutSpy).toBeCalledTimes(0);
});

it('should not call logout when error has no response (when axios errors occur before sending request)', async () => {
expect.assertions(1);

const logoutSpy = jest.fn();
jest.spyOn(config, 'getService').mockReturnValue({
login: jest.fn(),
subscribe: jest.fn(),
unsubscribe: jest.fn(),
getState: jest.fn(),
logout: logoutSpy
});

await authInterceptor({}).catch(() => undefined);

expect(logoutSpy).toBeCalledTimes(0);
});
});

describe('authFetch', () => {
Expand Down

0 comments on commit f2a9be8

Please sign in to comment.