Skip to main content
logto-authkit supports custom navigation to integrate seamlessly with your routing library, preventing full page reloads during authentication flows.

Why Custom Navigation?

By default, Logto uses window.location.href for navigation, which causes full page reloads. Custom navigation allows you to:
  • Use client-side routing (React Router, Next.js, etc.)
  • Maintain application state during auth flows
  • Provide a smoother user experience
  • Control navigation behavior in SPAs

Basic Setup

Pass a customNavigate function to the AuthProvider:

React Router Integration

React Router v6

React Router v5

Next.js Integration

App Router (Next.js 13+)

Pages Router (Next.js 12 and earlier)

The customNavigate function receives two parameters:
url
string
required
The URL to navigate to (can be relative or absolute)
options
NavigationOptions
Navigation options object:

How It Works

1

Provider Registration

When you pass customNavigate to AuthProvider, it registers your navigation function globally for the library.
2

Internal Navigation Calls

When logto-authkit needs to navigate (e.g., after sign-in, during callback), it uses your custom function instead of window.location.href.
3

Cleanup on Unmount

When the AuthProvider unmounts, it automatically unregisters the custom navigation function.

Implementation Details

The custom navigation is set using the setCustomNavigate utility from the library:
This ensures that:
  • The navigation function is available throughout the library
  • It’s properly cleaned up when the component unmounts
  • You can dynamically change the navigation function if needed

Best Practices

Always check if the URL is external (starts with http:// or https://) and use window.location.href for those cases. Authentication flows may redirect to external Logto servers.
Respect the options.replace parameter to allow the library to replace history entries when appropriate.
Make sure your customNavigate function is stable (use useCallback if needed) to prevent unnecessary re-renders.
Custom navigation works seamlessly with popup sign-in mode:
When using popup sign-in, the main window navigation is handled by your custom function, while the popup window uses its own navigation context.

Troubleshooting

Make sure your customNavigate function is properly handling relative URLs. Check that you’re not falling back to window.location.href for internal routes.
Check that your callback URL matches the route where you render <CallbackPage />. Mismatched URLs can cause redirect loops.

Next Steps

AuthProvider

Learn more about AuthProvider configuration

useAuth Hook

Learn more about the useAuth hook