Skip to main content
Protect routes and require authentication before allowing access to specific pages in your application.

Implementation

Usage examples

React Router v6

Next.js App Router

Advanced patterns

With redirect to original destination

With role-based access

With wallet requirement

Key concepts

  • Authentication check - Verifies user is logged in
  • Loading state - Shows loading while SDK initializes
  • Automatic redirect - Sends unauthenticated users to login
  • Preserve location - Can remember where user tried to go
  • Flexible protection - Can add additional requirements (roles, wallets, etc.)

Important notes

Always check the ready state before checking isAuthenticated to avoid redirecting users while MoonKey is still initializing.
Use replace prop in Navigate to prevent users from going back to protected routes using the browser back button.

Next steps

Authentication Flow

Build your login page

Dashboard

Create protected dashboard