Overview
TheCallbackPage component handles the authentication callback after users are redirected back from Logto. It processes the authentication code, exchanges it for tokens, and manages the post-authentication flow including popup and redirect scenarios.
Installation
Basic Usage
Create a callback route in your application:app/callback/page.tsx
Props
Additional CSS classes to apply to the container element.
Custom component to display while processing authentication.
Custom component to display after successful authentication.
Callback function executed after successful authentication, before redirect/close.
Callback function executed if authentication fails.
How It Works
TheCallbackPage component:
- Receives the auth code from Logto redirect URL
- Exchanges code for tokens using
useHandleSignInCallback()from@logto/react - Detects the flow type (popup vs. redirect)
- Handles completion:
- Popup flow: Sends message to parent window and closes
- Redirect flow: Redirects to home page (
/)
Flow Detection
The component automatically detects whether it’s handling a popup or redirect flow:- Checks if the window has an
opener(parent window) - Falls back to
sessionStorageflag for cross-origin scenarios
Popup Flow
For popup-based authentication:- Component detects it’s in a popup
- Processes authentication
- Sends
SIGNIN_SUCCESSmessage to parent window - Closes the popup
Fallback Mechanism
Ifwindow.opener is unavailable (some browsers clear it), falls back to localStorage:
The parent window listens for both
postMessage and localStorage events to handle popup completion.Redirect Flow
For full-page redirect authentication:- Component detects it’s NOT in a popup
- Processes authentication
- Redirects to home page (
/)
Examples
Basic Setup (Next.js App Router)
app/callback/page.tsx
With Custom Loading State
With Success Tracking
Custom Styling
With Custom Redirect
Default UI
If no custom components are provided, the callback page displays:Loading State
Success State
Session Storage Flag
For popup flows, your sign-in page should set a flag:app/signin/page.tsx
Error Handling
The component handles errors during authentication:- Invalid authorization code
- Token exchange failure
- Network errors
- CORS issues
Best Practices
Create a dedicated route
Create a dedicated route
Always use a dedicated route like
/callback or /auth/callback for handling authentication callbacks.Configure Logto redirect URI
Configure Logto redirect URI
Make sure your Logto application’s redirect URI matches your callback route exactly.
Handle both flows
Handle both flows
The component automatically handles both popup and redirect flows - no additional configuration needed.
Add error tracking
Add error tracking
Use the
onError callback to track authentication failures in your monitoring system.Customize user experience
Customize user experience
Provide custom loading and success components that match your application’s design.
Configuration in Logto
In your Logto application settings, add your callback URL:Troubleshooting
The component automatically cleans up the
simple_logto_popup_flow flag after successful authentication.Related
AuthProvider
Configure authentication provider
useAuth Hook
Access sign-in functionality
UserCenter
User menu with sign-in button
Route Protection
Protect authenticated routes