🐛(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:
@@ -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>;
|
||||
};
|
||||
|
||||
@@ -30,5 +30,10 @@ export const useAuth = () => {
|
||||
}
|
||||
}, [user, replace]);
|
||||
|
||||
return { user, authenticated: !!user, pathAllowed, ...authStates };
|
||||
return {
|
||||
user,
|
||||
authenticated: !!user && authStates.isSuccess,
|
||||
pathAllowed,
|
||||
...authStates,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user