Skip to main content
Prefer AI-assisted setup? Use our AI prompts for Next.js to automatically integrate Civic Auth using Claude, ChatGPT, or other AI assistants. Includes a step-by-step video tutorial!

Quick Start

Integrate Civic Auth into your Next.js application using the following steps (a working example is available in our github examples repo):
Important: Make sure your application is using Next.js version ^14.2.25 or ^15.2.3 (or higher). Earlier versions are affected by a security vulnerability (CVE-2025-29927) that may allow middleware to be bypassed.
This guide assumes you are using Typescript. Please adjust the snippets as needed to remove the types if you are using plain JS.
If you plan to use Web3 features, select “Auth + Web3” from the tabs below.

1. Add the Civic Auth Plugin

This is where you give your app the Client ID provided when you sign up at auth.civic.com. Important: The next.config.ts file goes in your project root directory, NOT in the src/ directory. The defaults should work out of the box for most customers, but if you want to configure your app, see below for details.
next.config.ts
Typescript support in configuration files was introduced in Next 15.If your config file is a JS file (next.config.mjs), make sure to change the extension to .ts, or remove the type information.

2. Create the Civic Auth API Route

This is where your app will handle login and logout requests. Important: This API route file goes in the src/ directory structure: src/app/api/auth/[...civicauth]/route.ts
route.ts
These steps apply to the App Router. If you are using the Pages Router, please contact Civic in our developer community for integration steps.

3. Middleware

Middleware is used to protect your backend routes, server components and server actions from unauthenticated requests. Using the Civic Auth middleware ensures that only logged-in users have access to secure parts of your service. Important: The middleware file goes in the src/ directory: src/middleware.ts
src/middleware.ts
Note: The default matcher does not include the root path (”/”). If you display authenticated elements (like UserButton) on the root page, add ”/” to the matcher array to ensure user state is refreshed during middleware prior to page load.

Middleware Chaining

If you are already using middleware in your Next.js app, then you can chain them with Civic Auth as follows:
src/middleware.ts

4. Frontend Integration

Add the Civic Auth context to your app to give your frontend access to the logged-in user. Important: Your layout file should be in the src/ directory structure (typically src/app/layout.tsx):
Unlike the pure React integration, you do not have to add your client ID again here!Make sure to create the Civic Auth API route, as it serves the essential PKCE code challenge.

Usage

Getting User Information on the Frontend

The Next.js integration can use all the components described in the React integration page, such as the UserButton , for showing a Sign-In button and displaying the username:
TitleBar.ts
or if you need to rollout your own button:
TitleBar.ts
or the useUser hook, for retrieving information about the user in code:
MyComponent.ts
You can also pass onSignIn and onSignOut callbacks to the useUser hook to trigger actions when the user signs in or out:
MyComponent.ts
See the React Usage page for more details.

Getting User Information on the Backend

Retrieve user information on backend code, such as in React Server Components, React Server Actions, or api routes using getUser:
For example, in a Next.js Server Component:
The name property is used as an example here, check out the React Usage page to see the entire basic user object structure.

Advanced Configuration

Civic Auth is a “low-code” solution, so most of the configuration takes place via the dashboard. Changes you make there will be updated automatically in your integration without any code changes. The only required parameter you need to provide is the client ID. The integration also offers the ability customize the library according to the needs of your Next.js app. For example, to restrict authentication checks to specific pages and routes in your app. You can do so inside next.config.js as follows:
next.config.ts

Deploying Behind Reverse Proxies

When deploying your Next.js application behind reverse proxies (such as Cloudfront + Vercel, AWS Application Load Balancer, or nginx), you may encounter authentication issues where redirects fail because the middleware detects internal origins instead of your public domain. Common symptoms:
  • Authentication works locally but fails in production
  • Users cannot log in after deployment
  • Redirect loops during the authentication process
  • Error messages about invalid redirect URLs
Solution: Set the baseUrl parameter in your Next.js configuration to specify your public-facing domain:
next.config.ts
The baseUrl parameter is only needed for reverse proxy deployments. Standard deployments (like direct Vercel hosting) work without this configuration.