Overview
TheuseAuth hook provides access to authentication state and handles automatic redirects based on authentication status and middleware configuration.
Signature
Parameters
Configuration options for authentication behavior
Returns
Authentication context object containing user state and authentication methods
Examples
Basic Usage
Protected Route
Guest-Only Route
Custom Navigation Options
Behavior
- The hook automatically handles redirects based on the
middlewareoption and authentication state - Redirects only occur after the loading state completes (
isLoadingUserisfalse) - When
middleware: 'auth'is set and user is not authenticated, redirects toredirectTo(defaults to/404) - When
middleware: 'guest'is set and user is authenticated, redirects toredirectIfAuthenticated - The hook memoizes options to prevent infinite re-renders when the options object reference changes
See Also
- AuthProvider - Provider component required to use this hook
- verifyAuth - Server authentication verification