Using the React SDK
You can use theuseMoonKey hook to determine if your user is authenticated and access their information.
Import
Available Properties
TheuseMoonKey hook provides several properties for managing authentication state:
boolean
Indicates whether the current user is authenticated or not.
boolean
Indicates whether the MoonKey SDK has finished initializing. Always check this before checking
isAuthenticated.User | null
The authenticated user object, or
null if no user is logged in.() => Promise<void>
Function to logout the current user.
(params?) => void
Function to initiate the authentication flow.
Basic Usage
Check if a user is authenticated:Common Patterns
Protected Routes
Redirect unauthenticated users to a login page:Conditional Rendering
Show different UI based on authentication state:Authentication Wrapper Component
Create a reusable wrapper for protected content:Loading States
Handle different loading and authentication states:Accessing User Information
Once authenticated, you can access detailed user information:Logout
Log out the current user:With Next.js App Router
Handle authentication state in Next.js App Router:With React Router
Protect routes with React Router:Server-Side Authentication
For server-side authentication checks, use the REST API to verify sessions:Best Practices
Always Check Ready State
Always verify the SDK is ready before checking authentication status:Handle All States
Consider all possible states in your UI:Use useEffect for Side Effects
UseuseEffect for redirects and other side effects:
Avoid Unnecessary Re-renders
Destructure only what you need:Common Patterns Summary
Related Resources
Email Authentication
Implement email OTP authentication
OAuth Login
Set up Google and Apple OAuth
Session Management
Learn about session handling
Quick Start
Get started with MoonKey