Skip to content

Commit f1844f2

Browse files
committed
🚧(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 bf99997 commit f1844f2

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,24 @@ import { User } from './types';
99
* This function is called during frontend initialization to check
1010
* the user's authentication status through a session cookie.
1111
*
12+
* If the user is not logged in, the api returns a 401 error.
13+
* Here our wrapper just returns false in that case, without triggering an error:
14+
* this is done to prevent unnecessary query retries with react query
15+
*
1216
* @async
1317
* @function getMe
1418
* @throws {Error} Throws an error if the API request fails.
1519
* @returns {Promise<User>} A promise that resolves to the user data.
1620
*/
17-
export const getMe = async (): Promise<User> => {
21+
export const getMe = async (): Promise<User | boolean> => {
1822
const response = await fetchAPI(`users/me/`);
19-
if (!response.ok) {
23+
24+
// we assume that a 401 means the user is not logged in
25+
if (!response.ok && response.status == 401) {
26+
return false
27+
}
28+
29+
if (!response.ok && response.status != 401) {
2030
throw new APIError(
2131
`Couldn't fetch user data: ${response.statusText}`,
2232
await errorCauses(response),

src/frontend/apps/impress/src/features/auth/hooks/useAuth.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const useAuth = () => {
3333

3434
return {
3535
user,
36-
authenticated: !!user && authStates.isSuccess,
36+
authenticated: authStates.isSuccess ? user !== false : undefined,
3737
pathAllowed,
3838
...authStates,
3939
};

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import { useCallback, useMemo, useRef } from 'react';
22
import { useTranslation } from 'react-i18next';
33

44
import { useConfig } from '@/core';
5-
import { useAuthQuery } from '@/features/auth/api';
65
import { useChangeUserLanguage } from '@/features/language/api/useChangeUserLanguage';
76
import { getMatchingLocales } from '@/features/language/utils/locale';
87
import { availableFrontendLanguages } from '@/i18n/initI18n';
8+
import { useAuth } from "@/features/auth";
99

1010
export const useLanguageSynchronizer = () => {
1111
const { data: conf, isSuccess: confInitialized } = useConfig();
12-
const { data: user, isSuccess: userInitialized } = useAuthQuery();
12+
const { user, authenticated: userInitialized } = useAuth();
1313
const { i18n } = useTranslation();
1414
const { mutateAsync: changeUserLanguage } = useChangeUserLanguage();
1515
const languageSynchronizing = useRef(false);

0 commit comments

Comments
 (0)