useAuth
Overview
The useAuth hook provides functionality for managing user authentication status and retrieving user authentication data.
Functionality
- Retrieves authentication data from local storage, including user token and user information.
- Verifies authentication token's validity through API call.
- Returns authenticated user data
Usage
import { useAuth } from 'path/to/useAuth';
const Component = () => {
const { data: authData, isLoading, isError } = useAuth();
if (isLoading) {
return <LoadingSpinner />;
}
if (isError) {
return <ErrorScreen />;
}
return <AuthenticatedComponent user={authData.user} />;
};
Additional Details
- It queries
verifyfunction from@lunchbox/apito verify the authentication token's validity. - Authentication data is retrieved from local storage using the
AUTH_STORAGE_KEYconstant. - The hook returns authenticated user data, excluding token, to maintain security.
retryOnMount,retry, andrefetchOnWindowFocusare configured to control the hook's behavior.staleTimeoption is set toInfinityto ensure that the authentication data remains valid throughout the user session.