Skip to content

Commit 2a7ffff

Browse files
lebaudantoineAntoLC
authored andcommitted
⚡️(frontend) prevent authentication retry on 401 responses
Stop retry attempts when receiving 401 Unauthorized from /me endpoint since this clearly indicates authentication status. The original purpose of the /me call is simply to determine if user is authenticated, and a 401 provides sufficient information. Prevents unnecessary network requests caused by React Query's automatic retry behavior when re-raising exceptions, which was hiding the 401 status. Improves performance and reduces server load during authentication failures.
1 parent ff8275f commit 2a7ffff

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

src/frontend/apps/impress/src/core/AppProvider.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { useResponsiveStore } from '@/stores/';
99

1010
import { ConfigProvider } from './config/';
1111

12+
export const DEFAULT_QUERY_RETRY = 1;
13+
1214
/**
1315
* QueryClient:
1416
* - defaultOptions:
@@ -19,7 +21,7 @@ import { ConfigProvider } from './config/';
1921
const defaultOptions = {
2022
queries: {
2123
staleTime: 1000 * 60 * 3,
22-
retry: 1,
24+
retry: DEFAULT_QUERY_RETRY,
2325
},
2426
};
2527
const queryClient = new QueryClient({

src/frontend/apps/impress/src/features/auth/api/useAuthQuery.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { UseQueryOptions, useQuery } from '@tanstack/react-query';
22

33
import { APIError, errorCauses, fetchAPI } from '@/api';
4+
import { DEFAULT_QUERY_RETRY } from '@/core';
45

56
import { User } from './types';
67

8+
type UserResponse = User | null;
9+
710
/**
811
* Asynchronously retrieves the current user's data from the API.
912
* This function is called during frontend initialization to check
@@ -14,8 +17,13 @@ import { User } from './types';
1417
* @throws {Error} Throws an error if the API request fails.
1518
* @returns {Promise<User>} A promise that resolves to the user data.
1619
*/
17-
export const getMe = async (): Promise<User> => {
20+
export const getMe = async (): Promise<UserResponse> => {
1821
const response = await fetchAPI(`users/me/`);
22+
23+
if (response.status === 401) {
24+
return null;
25+
}
26+
1927
if (!response.ok) {
2028
throw new APIError(
2129
`Couldn't fetch user data: ${response.statusText}`,
@@ -28,12 +36,19 @@ export const getMe = async (): Promise<User> => {
2836
export const KEY_AUTH = 'auth';
2937

3038
export function useAuthQuery(
31-
queryConfig?: UseQueryOptions<User, APIError, User>,
39+
queryConfig?: UseQueryOptions<UserResponse, APIError, UserResponse>,
3240
) {
33-
return useQuery<User, APIError, User>({
41+
return useQuery<UserResponse, APIError, UserResponse>({
3442
queryKey: [KEY_AUTH],
3543
queryFn: getMe,
3644
staleTime: 1000 * 60 * 15, // 15 minutes
45+
retry: (failureCount, error) => {
46+
// we assume that a 401 means the user is not logged in
47+
if (error.status == 401) {
48+
return false;
49+
}
50+
return failureCount < DEFAULT_QUERY_RETRY;
51+
},
3752
...queryConfig,
3853
});
3954
}

src/frontend/apps/impress/src/features/language/hooks/useSynchronizedLanguage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const useSynchronizedLanguage = () => {
2222
);
2323

2424
const changeBackendLanguage = useCallback(
25-
async (language: string, user?: User) => {
25+
async (language: string, user?: User | null) => {
2626
const closestBackendLanguage = getMatchingLocales(
2727
availableBackendLanguages,
2828
[language],
@@ -52,7 +52,7 @@ export const useSynchronizedLanguage = () => {
5252
);
5353

5454
const changeLanguageSynchronized = useCallback(
55-
async (language: string, user?: User) => {
55+
async (language: string, user?: User | null) => {
5656
if (!isSynchronizingLanguage.current) {
5757
isSynchronizingLanguage.current = true;
5858
await changeFrontendLanguage(language);

0 commit comments

Comments
 (0)