From 315c2c2c43501099dd826190c82ebbccb4980c85 Mon Sep 17 00:00:00 2001 From: Anthony LC Date: Fri, 28 Feb 2025 11:20:43 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B(frontend)=20improve=20authenticate?= =?UTF-8?q?d=20state?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It can happen that the user is authenticated then the token is expired. The authenticated state should be updated to false in this case. --- .../apps/impress/src/features/auth/api/useAuthQuery.tsx | 7 +++++-- .../apps/impress/src/features/auth/hooks/useAuth.tsx | 7 ++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/frontend/apps/impress/src/features/auth/api/useAuthQuery.tsx b/src/frontend/apps/impress/src/features/auth/api/useAuthQuery.tsx index 714cc9e0..026beec9 100644 --- a/src/frontend/apps/impress/src/features/auth/api/useAuthQuery.tsx +++ b/src/frontend/apps/impress/src/features/auth/api/useAuthQuery.tsx @@ -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 => { 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; }; diff --git a/src/frontend/apps/impress/src/features/auth/hooks/useAuth.tsx b/src/frontend/apps/impress/src/features/auth/hooks/useAuth.tsx index 6a13d514..a697a3cd 100644 --- a/src/frontend/apps/impress/src/features/auth/hooks/useAuth.tsx +++ b/src/frontend/apps/impress/src/features/auth/hooks/useAuth.tsx @@ -30,5 +30,10 @@ export const useAuth = () => { } }, [user, replace]); - return { user, authenticated: !!user, pathAllowed, ...authStates }; + return { + user, + authenticated: !!user && authStates.isSuccess, + pathAllowed, + ...authStates, + }; };