Skip to content

Commit 17c5cb7

Browse files
authored
support unique csrftoken per env (#382)
* support unique csrftoken per env * fix missing param
1 parent 6182b98 commit 17c5cb7

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

react/src/AppRouter.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ const ProtectedRoute: React.FC<ProtectedRouteProps> = ({ children }) => {
4747
const basePath = getBasePath();
4848

4949
const rootLoader = (queryClient: QueryClient) => async () => {
50-
const { geoapiUrl } = computeAppConfiguration(basePath);
50+
const { geoapiUrl, geoapiEnv } = computeAppConfiguration(basePath);
5151
const data = await queryClient.ensureQueryData(
52-
getAuthenticatedUserQuery(geoapiUrl)
52+
getAuthenticatedUserQuery(geoapiUrl, geoapiEnv)
5353
);
5454
return data;
5555
};

react/src/hooks/user/useAuthenticatedUser.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ import { AxiosError } from 'axios';
22
import { isTokenValid } from '@hazmapper/utils/authUtils';
33
import { useSuspenseQuery, queryOptions } from '@tanstack/react-query';
44
import { getApiClient } from '@hazmapper/requests';
5-
import { AuthState, AuthToken } from '@hazmapper/types';
5+
import { AuthState, AuthToken, ApiService } from '@hazmapper/types';
66
import { useAppConfiguration } from '@hazmapper/hooks';
77

88
export type TAuthenticatedUserResponse = {
99
username: string | null;
1010
authToken: AuthToken | null;
1111
};
1212

13-
async function getAuthenticatedUser(baseUrl: string) {
14-
const apiClient = getApiClient();
13+
async function getAuthenticatedUser(baseUrl: string, geoapiEnv: string) {
14+
const apiClient = getApiClient(ApiService.Geoapi, geoapiEnv);
1515
const endpoint = `${baseUrl}/auth/user/`;
1616
try {
1717
const res = await apiClient.get<TAuthenticatedUserResponse>(endpoint);
@@ -25,10 +25,10 @@ async function getAuthenticatedUser(baseUrl: string) {
2525
}
2626
}
2727

28-
export const getAuthenticatedUserQuery = (baseUrl: string) =>
28+
export const getAuthenticatedUserQuery = (baseUrl: string, geoapiEnv: string) =>
2929
queryOptions({
3030
queryKey: ['authenticated-user'],
31-
queryFn: () => getAuthenticatedUser(baseUrl),
31+
queryFn: () => getAuthenticatedUser(baseUrl, geoapiEnv),
3232
staleTime: 1000 * 60 * 60 * 4 - 1000 * 60 * 5, // 3hrs 55 minutes stale time
3333
refetchInterval: 1000 * 60 * 30, // Refetch every 30 minutes
3434
refetchIntervalInBackground: true,
@@ -44,8 +44,8 @@ export const getAuthenticatedUserQuery = (baseUrl: string) =>
4444
});
4545

4646
function useAuthenticatedUser() {
47-
const { geoapiUrl } = useAppConfiguration();
48-
return useSuspenseQuery(getAuthenticatedUserQuery(geoapiUrl));
47+
const { geoapiUrl, geoapiEnv } = useAppConfiguration();
48+
return useSuspenseQuery(getAuthenticatedUserQuery(geoapiUrl, geoapiEnv));
4949
}
5050

5151
export default useAuthenticatedUser;

react/src/requests.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@ import {
2020
import { ApiService, AppConfiguration, AuthToken } from '@hazmapper/types';
2121
import { HASHED_SESSION } from '@hazmapper/utils/requestUtils';
2222

23-
export const getApiClient = (apiService: ApiService = ApiService.Geoapi) => {
23+
export const getApiClient = (
24+
apiService: ApiService = ApiService.Geoapi,
25+
geoapiEnv: string
26+
) => {
2427
const axiosConfig = {
2528
timeout: 60 * 1000, // 1 minute
2629
};
2730
if (apiService === ApiService.Geoapi) {
2831
Object.assign(axiosConfig, {
29-
xsrfCookieName: 'csrftoken',
30-
xsrfHeaderName: 'x-csrftoken',
32+
xsrfCookieName: `csrftoken-${geoapiEnv}`,
33+
xsrfHeaderName: `x-csrftoken-${geoapiEnv}`,
3134
withCredentials: true, // Ensure cookies are sent with requests
3235
withXSRFToken: true,
3336
});
@@ -135,8 +138,8 @@ export function useGet<ResponseType, TransformedResponseType = ResponseType>({
135138
transform,
136139
prefetch,
137140
}: UseGetParams<ResponseType, TransformedResponseType>) {
138-
const client = getApiClient(apiService);
139141
const configuration = useAppConfiguration();
142+
const client = getApiClient(apiService, configuration.geoapiEnv);
140143
const { accessToken: mapillaryAuthToken } = useMapillaryToken();
141144

142145
const isPublicRoute = useIsPublicProjectRoute();
@@ -186,8 +189,8 @@ export function usePost<RequestType, ResponseType>({
186189
options = {},
187190
apiService = ApiService.Geoapi,
188191
}: UsePostParams<RequestType, ResponseType>) {
189-
const client = getApiClient(apiService);
190192
const configuration = useAppConfiguration();
193+
const client = getApiClient(apiService, configuration.geoapiEnv);
191194

192195
const baseUrl = getBaseApiUrl(apiService, configuration);
193196

@@ -239,8 +242,8 @@ export function useDelete<ResponseType, Variables>({
239242
options = {},
240243
apiService = ApiService.Geoapi,
241244
}: UseDeleteParams<ResponseType, Variables>) {
242-
const client = getApiClient(apiService);
243245
const configuration = useAppConfiguration();
246+
const client = getApiClient(apiService, configuration.geoapiEnv);
244247

245248
const baseUrl = getBaseApiUrl(apiService, configuration);
246249
const isTapisTokenRequest = usesTapisToken(apiService);
@@ -282,8 +285,8 @@ export function usePut<RequestType, ResponseType>({
282285
options = {},
283286
apiService = ApiService.Geoapi,
284287
}: UsePostParams<RequestType, ResponseType>) {
285-
const client = getApiClient(apiService);
286288
const configuration = useAppConfiguration();
289+
const client = getApiClient(apiService, configuration.geoapiEnv);
287290

288291
const baseUrl = getBaseApiUrl(apiService, configuration);
289292

0 commit comments

Comments
 (0)