Skip to main content

Overview

The CallbackPage 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

className
string
Additional CSS classes to apply to the container element.
loadingComponent
React.ReactNode
Custom component to display while processing authentication.
successComponent
React.ReactNode
Custom component to display after successful authentication.
onSuccess
() => void
Callback function executed after successful authentication, before redirect/close.
onError
(error: Error) => void
Callback function executed if authentication fails.

How It Works

The CallbackPage component:
  1. Receives the auth code from Logto redirect URL
  2. Exchanges code for tokens using useHandleSignInCallback() from @logto/react
  3. Detects the flow type (popup vs. redirect)
  4. 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 sessionStorage flag for cross-origin scenarios
For popup-based authentication:
  1. Component detects it’s in a popup
  2. Processes authentication
  3. Sends SIGNIN_SUCCESS message to parent window
  4. Closes the popup

Fallback Mechanism

If window.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:
  1. Component detects it’s NOT in a popup
  2. Processes authentication
  3. 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:
Common errors:
  • Invalid authorization code
  • Token exchange failure
  • Network errors
  • CORS issues

Best Practices

Always use a dedicated route like /callback or /auth/callback for handling authentication callbacks.
Make sure your Logto application’s redirect URI matches your callback route exactly.
The component automatically handles both popup and redirect flows - no additional configuration needed.
Use the onError callback to track authentication failures in your monitoring system.
Provide custom loading and success components that match your application’s design.

Configuration in Logto

In your Logto application settings, add your callback URL:
For local development:

Troubleshooting

Popup doesn’t close: Ensure your sign-in page sets the simple_logto_popup_flow sessionStorage flag when ?popup=true is in the URL.
Infinite redirect loop: Check that your Logto redirect URI exactly matches your callback route.
CORS errors: Ensure your Logto application’s allowed origins include your application’s domain.
The component automatically cleans up the simple_logto_popup_flow flag after successful authentication.

AuthProvider

Configure authentication provider

useAuth Hook

Access sign-in functionality

UserCenter

User menu with sign-in button

Route Protection

Protect authenticated routes