Make sure you’ve completed the installation steps before starting this quickstart.
Step 1: Create the callback route
AuthKit redirects users back to your app after authentication. Create a route handler to process this callback. Create a file atapp/callback/route.ts:
app/callback/route.ts
Customize the redirect path
By default, users are redirected to/ after signing in. Customize this with the returnPathname option:
app/callback/route.ts
Handle successful authentication
Use theonSuccess callback to save OAuth tokens or perform other actions:
app/callback/route.ts
Step 2: Set up middleware
Middleware manages sessions and handles authentication for protected routes.- Next.js 16+
- Next.js 15 and earlier
Create a
proxy.ts file in your project root:proxy.ts
Step 3: Wrap your app with AuthKitProvider
TheAuthKitProvider enables client-side authentication hooks and handles auth edge cases.
Update your root layout:
app/layout.tsx
Optimize with server-side data
Avoid an extra server action call by passing initial auth data:app/layout.tsx
Step 4: Add authentication to your pages
Now you can access user data in both server and client components.Server component example
Create a homepage with sign-in/sign-out functionality:app/page.tsx
Client component example
For client components, use theuseAuth hook:
app/components/user-menu.tsx
Require authentication
Force users to sign in before accessing a page:Step 5: Test authentication
Start your development server and test the authentication flow:Navigate to your app
Open http://localhost:3000 in your browser
Optional: Add user impersonation UI
Display a banner when impersonating users for customer support:app/layout.tsx
Impersonation component automatically shows a banner with user details and a “Stop impersonating” button when an admin is impersonating a user.
Next steps
Access tokens
Learn how to get access tokens for API calls to external services
Organization switching
Enable users to switch between different organizations
Feature flags
Use feature flags to control access to features for specific users
API key validation
Secure your API endpoints with WorkOS API keys
Common issues
NEXT_REDIRECT error in try/catch blocks
NEXT_REDIRECT error in try/catch blocks
Don’t wrap
withAuth({ ensureSignedIn: true }) in try/catch. Next.js redirects must be called outside try/catch blocks. Use the return value to check for authentication instead:UnhandledSchemeError with node:crypto
UnhandledSchemeError with node:crypto
This error occurs when importing server-side code in client components. Make sure you’re using
useAuth() in client components and withAuth() in server components. Check that all files using withAuth() are server components (no 'use client' directive).Styles breaking with catch-all matcher
Styles breaking with catch-all matcher
Catch-all middleware matchers can intercept CSS and image requests. Use a more specific matcher or exclude Next.js internal paths:
Redirect URI mismatch
Redirect URI mismatch
Ensure your
NEXT_PUBLIC_WORKOS_REDIRECT_URI exactly matches the callback route path and the redirect URI configured in your WorkOS dashboard. The URI must include the protocol (http:// or https://) and port number for local development.