logto-authkit supports custom navigation to integrate seamlessly with your routing library, preventing full page reloads during authentication flows.Documentation Index
Fetch the complete documentation index at: https://docs.ouim.me/llms.txt
Use this file to discover all available pages before exploring further.
Why Custom Navigation?
By default, Logto useswindow.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 acustomNavigate 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)
Navigation Options
ThecustomNavigate function receives two parameters:
The URL to navigate to (can be relative or absolute)
Navigation options object:
How It Works
Provider Registration
When you pass
customNavigate to AuthProvider, it registers your navigation function globally for the library.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.Implementation Details
The custom navigation is set using thesetCustomNavigate utility from the library:
- 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
Handle External URLs
Handle External URLs
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.Support Replace Option
Support Replace Option
Respect the
options.replace parameter to allow the library to replace history entries when appropriate.Use Stable Functions
Use Stable Functions
Make sure your
customNavigate function is stable (use useCallback if needed) to prevent unnecessary re-renders.Popup Sign-In
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
Full page reload after sign-in
Full page reload after sign-in
Make sure your
customNavigate function is properly handling relative URLs. Check that you’re not falling back to window.location.href for internal routes.Navigation not working
Navigation not working
Infinite redirects
Infinite redirects
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