Skip to content

Commit

Permalink
Merge pull request #279 from Code-4-Community/AH.TempAxiosFix
Browse files Browse the repository at this point in the history
  • Loading branch information
huang0h authored Sep 20, 2023
2 parents fdc00a3 + 1f255ad commit 8252df1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import history from './history';
import FAQ from './containers/faq';
import Email from './containers/email';
import AddSites from './containers/addSites';
import { addAxiosInterceptors } from './auth/axios';

const AppLayout = styled(Layout)`
min-height: 100vh;
Expand Down Expand Up @@ -87,6 +88,9 @@ export interface MapStateProps {
readonly lng: number;
}

// temp fix - adding axios interceptors here as they aren't don't get added when in auth/axios.ts
addAxiosInterceptors();

const App: React.FC = () => {
const dispatch = useDispatch();

Expand Down
26 changes: 14 additions & 12 deletions src/auth/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,18 @@ export const responseErrorInterceptor = async (
return Promise.reject(error);
};

AppAxiosInstance.interceptors.response.use(
(response) => response,
responseErrorInterceptor,
);
export const addAxiosInterceptors = () => {
AppAxiosInstance.interceptors.response.use(
(response) => response,
responseErrorInterceptor,
);

AppAxiosInstance.interceptors.request.use((config) => {
const tokens: UserAuthenticationReducerState['tokens'] =
store.getState().authenticationState.tokens;
if (asyncRequestIsComplete(tokens)) {
config.headers['X-Access-Token'] = tokens.result.accessToken;
}
return config;
});
AppAxiosInstance.interceptors.request.use((config) => {
const tokens: UserAuthenticationReducerState['tokens'] =
store.getState().authenticationState.tokens;
if (asyncRequestIsComplete(tokens)) {
config.headers['X-Access-Token'] = tokens.result.accessToken;
}
return config;
});
};

0 comments on commit 8252df1

Please sign in to comment.