🐛(frontend) improve authenticated state

It can happen that the user is authenticated
then the token is expired. The authenticated
state should be updated to false in this case.
This commit is contained in:
Anthony LC
2025-02-28 11:20:43 +01:00
committed by Anthony LC
parent e442908c50
commit 315c2c2c43
2 changed files with 11 additions and 3 deletions

View File

@@ -1,6 +1,6 @@
import { UseQueryOptions, useQuery } from '@tanstack/react-query';
import { APIError, fetchAPI } from '@/api';
import { APIError, errorCauses, fetchAPI } from '@/api';
import { User } from './types';
@@ -17,7 +17,10 @@ import { User } from './types';
export const getMe = async (): Promise<User> => {
const response = await fetchAPI(`users/me/`);
if (!response.ok) {
throw new Error(`Couldn't fetch user data: ${response.statusText}`);
throw new APIError(
`Couldn't fetch user data: ${response.statusText}`,
await errorCauses(response),
);
}
return response.json() as Promise<User>;
};

View File

@@ -30,5 +30,10 @@ export const useAuth = () => {
}
}, [user, replace]);
return { user, authenticated: !!user, pathAllowed, ...authStates };
return {
user,
authenticated: !!user && authStates.isSuccess,
pathAllowed,
...authStates,
};
};