How to add AuthKit to a Next.js app
Go from no auth to a full hosted sign-in flow, either in one command or step by step.
Authentication is one of those features that looks small on a roadmap and then eats a sprint. Sign-up and sign-in are the easy part. Then come password resets, email verification, MFA, social login, session refresh, and the security review that follows all of it.
AuthKit hands you those screens and the session handling behind them, so your Next.js app gets a production-ready auth flow without you designing a single form. There are two ways to wire it up, and this tutorial covers both. The CLI installer does the integration for you in about two minutes and is where most people should start. The manual walkthrough follows, for when you want to understand or control every line.
Before you start
You'll need:
- A WorkOS account
- Your WorkOS API key and client ID
- Node.js 20 or later
- A Next.js app using the App Router
The fastest option: the CLI installer
The WorkOS CLI ships an AI installer that detects your framework, installs the right SDK, writes the routes, configures your dashboard, and validates the build. Run one command from your project root:
The installer will:
- Detect your framework and version from your dependencies and file structure
- Open your browser to authenticate your WorkOS account
- Configure your dashboard, including redirect URIs, CORS origins, and homepage URL
- Install
@workos-inc/authkit-nextjs - Create
/app/callback/route.tsandproxy.ts, and wrap your root layout inAuthKitProvider - Write your keys to
.env.local - Run your build to confirm everything compiles
Here's what a run looks like:
If you already have middleware or configuration in place, the installer composes with it rather than replacing it. Run git diff afterward to review every change.
A few useful flags:
Make sure your project builds cleanly before you run the installer. Pre-existing build errors will fail validation.
Once the installer finishes, skip ahead to Validate the flow. If you'd rather wire things up yourself, keep reading.
Manual integration
Step 1: Install the SDK
Step 2: Configure a redirect URI
The redirect URI is the callback endpoint WorkOS sends users to after they authenticate. That endpoint exchanges the authorization code for an authenticated user object.
In the WorkOS Dashboard, open your application under Applications, go to the Redirects tab, and add a redirect URI. Use http://localhost:3000/callback for local development.
While you're on that tab, set a Sign-out URI as well. Without one, users see an error when they sign out.
Step 3: Configure an initiate login URL
Sign-in requests are meant to start in your app, but they don't always. Someone might bookmark the hosted sign-in page, or arrive there from a password reset or invitation email.
When AuthKit detects a sign-in request that didn't originate in your app, it redirects to your initiate login URL, an endpoint you define that kicks off an AuthKit sign-in. Password reset and invitation details survive the redirect, so users land in the right place, as long as your initiate login URL starts an AuthKit sign-in rather than rendering your own sign-in page.
Set it on the same Redirects tab in the dashboard.
Step 4: Set your secrets
Add these to .env.local:
The NEXT_PUBLIC prefix on the redirect URI makes the value available in edge functions and proxy configurations, which matters for things like Vercel preview deployments.
The cookie password encrypts your session cookies and must be at least 32 characters. Generate one with:
Step 5: Wrap your app in the provider
AuthKitProvider handles auth edge cases and is required around your app layout.
Step 6: Add the proxy
Next.js needs a proxy to determine which routes require authentication. (This was called middleware before Next 16.)
You have two options. Use authkitMiddleware if your proxy only handles auth. Use the composable authkit method if your proxy needs to do other work too.
Option A: the complete proxy
With authkitMiddleware, session management and redirects are handled for you. There are two modes.
Page based auth. Protected routes are decided by where you call withAuth({ ensureSignedIn: true }), covered in step 9.
Middleware auth. Every route is protected by default, with exceptions listed explicitly.
Here the home page is public, and /account and everything under it requires a signed-in user.
Option B: the composable proxy
The authkit method manages the session and leaves route protection to you.
Preserving the AuthKit headers on redirects matters. Drop them and you drop the session cookie.
Step 7: Add the callback route
This route must match both your NEXT_PUBLIC_WORKOS_REDIRECT_URI and the redirect URI in your dashboard.
Step 8: Add the login route
This is the endpoint you configured as your initiate login URL. It generates an AuthKit authorization URL server side and redirects the user to it.
Step 9: Read the authenticated user
AuthKit works in both server and client components.
In a server component, use withAuth:
In a client component, use the useAuth hook:
Step 10: Protect routes
For pages that require a signed-in user, pass ensureSignedIn. Anyone without a session is redirected to AuthKit automatically.
In a server component:
In a client component:
Step 11: Let users sign out
Call signOut from a server action. After signing out, users land on the sign-out URI you configured in the dashboard.
Validate the flow
Start your dev server:
Go to localhost:3000 and sign up for an account. Sign out, then sign back in with the credentials you just created. The new user should appear under Users in the WorkOS Dashboard.
Troubleshooting
- Users see an error on sign-out. You haven't configured a sign-out URI in the dashboard. Add one on the Redirects tab.
- Sessions don't persist. Check that
WORKOS_COOKIE_PASSWORDis at least 32 characters, and that your composable proxy forwards AuthKit'sset-cookieheaders on redirects. - Callback errors after sign-in. Your callback route,
NEXT_PUBLIC_WORKOS_REDIRECT_URI, and the dashboard redirect URI all have to match exactly. - Something else. Run
workos doctorfrom your project root. It checks your SDK version, environment configuration, connectivity, dashboard settings, and auth patterns.
What's next
- Give your coding agent WorkOS context with
workos skills install, supported in Claude Code, Codex, Cursor, and Goose - Read up on session management
- Match AuthKit to your product with branding
- Browse the example apps