Overview
TheAuthProvider component is the core wrapper for logto-authkit authentication. It manages authentication state, handles sign-in/sign-out operations, and provides auth context to your entire application.
Installation
TheAuthProvider is included in the logto-authkit package:
Basic Usage
Wrap your application withAuthProvider at the root level:
Props
Logto configuration object containing authentication settings.
The URL to redirect to after authentication. Defaults to current page.
Enable popup-based sign-in instead of full page redirects.
Custom navigation function for framework-specific routing (e.g., Next.js router).
Your application components that need access to authentication context.
Features
Automatic State Management
TheAuthProvider automatically:
- Fetches and updates user information when authentication state changes
- Handles JWT token storage in cookies
- Syncs authentication across browser tabs and windows
- Refreshes auth state on window focus
- Manages loading states during authentication operations
Popup Sign-In Support
WhenenablePopupSignIn is enabled, sign-in operations open in a popup window instead of redirecting the entire page:
Popup sign-in provides a better user experience as it doesn’t interrupt the user’s current page state.
Cross-Tab Synchronization
Authentication state is automatically synchronized across browser tabs:- Sign in on one tab → all tabs update
- Sign out on one tab → all tabs update
- Uses localStorage events and custom event dispatching
Error Handling
The provider includes robust error handling:- Automatic logout on invalid/expired tokens
- Rate limiting to prevent excessive API calls
- Graceful fallback for popup authentication failures
Advanced Usage
Next.js App Router
app/layout.tsx
With Custom Navigation (Next.js)
Context Value
TheAuthProvider exposes the following context value (accessible via useAuth hook):
Best Practices
Place at the root of your application
Place at the root of your application
Always wrap your entire application with
AuthProvider to ensure all components have access to authentication context.Use environment variables for config
Use environment variables for config
Store sensitive configuration like
endpoint and appId in environment variables, not hardcoded in your source.Enable popup sign-in for better UX
Enable popup sign-in for better UX
For better user experience, enable
enablePopupSignIn to avoid full page redirects during authentication.Handle SSR properly
Handle SSR properly
The provider includes built-in SSR handling with
ClientOnly wrapper. No additional configuration needed for Next.js.Troubleshooting
The provider automatically handles token refresh and manages authentication state across page navigations.
Related
useAuth Hook
Access authentication state and methods
CallbackPage
Handle authentication callbacks
UserCenter
Pre-built user menu component
Route Protection
Protect routes and pages