Skip to main content
Check user authentication status throughout your application. Throughout your app, you may want to gate certain user experiences based on whether the current user is authenticated or not. MoonKey makes it easy to check your user’s authentication status and handle it appropriately.

Using the React SDK

You can use the useMoonKey hook to determine if your user is authenticated and access their information.

Import

Available Properties

The useMoonKey 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.
Before determining a user’s auth status, you should verify that MoonKey has fully initialized and is ready.

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

Use useEffect for redirects and other side effects:

Avoid Unnecessary Re-renders

Destructure only what you need:

Common Patterns Summary

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