Skip to main content

Overview

The useAuth hook provides access to authentication state and handles automatic redirects based on authentication status and middleware configuration.

Signature

Parameters

options
AuthOptions
Configuration options for authentication behavior

Returns

AuthContextType
object
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 middleware option and authentication state
  • Redirects only occur after the loading state completes (isLoadingUser is false)
  • When middleware: 'auth' is set and user is not authenticated, redirects to redirectTo (defaults to /404)
  • When middleware: 'guest' is set and user is authenticated, redirects to redirectIfAuthenticated
  • The hook memoizes options to prevent infinite re-renders when the options object reference changes

See Also