---
title: Use Neon Auth with Next.js (UI Components)
subtitle: Set up authentication in Next.js using pre-built UI components
enableTableOfContents: true
updatedOn: '2025-12-11T10:46:56.814Z'
layout: wide
---
Enable Auth in your [Neon project](https://console.neon.tech) and copy your Auth URL from Configuration.
**Console path:** Project → Branch → Auth → Configuration

Install the Neon SDK into your Next.js app.
_If you don't have a Next.js project_
```bash
npx create-next-app@latest my-app --yes
cd my-app
```
```bash
npm install @neondatabase/neon-js
```
Create a `.env` file in your project root and add your Auth URL:
Replace the URL with your actual Auth URL from the Neon Console.
```bash
NEON_AUTH_BASE_URL=https://ep-xxx.neonauth.us-east-1.aws.neon.tech/neondb/auth
```
We need to mount the `authApiHandler` handler to the auth API route. All Neon Auth APIs will be routed through this handler. Create a route file inside `/api/auth/[...path]` directory and add the following code:
```typescript
import { authApiHandler } from '@neondatabase/neon-js/auth/next';
export const { GET, POST } = authApiHandler();
```
The `neonAuthMiddleware()` ensures that user is authenticated before the request reaches your page components or API routes. Create `proxy.ts` file in your project root:
```typescript
import { neonAuthMiddleware } from "@neondatabase/neon-js/auth/next";
export default neonAuthMiddleware({
// Redirects unauthenticated users to sign-in page
loginUrl: "/auth/sign-in",
});
export const config = {
matcher: [
// Protected routes requiring authentication
"/account/:path*",
],
};
```
Your Next.js project is now fully configured to use Neon Auth. Now, lets proceed with setting up the Auth UI Provider and wrap your layout with auth context.
The Auth UI components need access to auth APIs. Lets first create the auth client in `lib/auth/client.ts` file then we pass it to `NeonAuthUIProvider`
```tsx
'use client';
import { createAuthClient } from '@neondatabase/neon-js/auth/next';
export const authClient = createAuthClient();
```
The `NeonAuthUIProvider` component wraps your application with authentication context and provides essential hooks and auth methods required by auth components throughout your app. To make authentication globally accessible, wrap your entire app with `NeonAuthUIProvider`.
Copy and pase the following code into your `app/layout.tsx` file.
The `NeonAuthUIProvider` can be fully customized with settings you have configured in Neon Console. For example:
- Add social providers like Google, Github, and Vercel on sign-in page
- Allow your users to create and manage organizations in `/account/organizations`
- Localization support
Example: Adding optional props
```tsx
{children}
```
```tsx
import { authClient } from '@/lib/auth/client'; // [!code ++]
import { NeonAuthUIProvider, UserButton } from '@neondatabase/neon-js/auth/react/ui'; // [!code ++]
import type { Metadata } from "next";
import { Geist, Geist_Mono } from "next/font/google";
import "./globals.css";
const geistSans = Geist({
variable: "--font-geist-sans",
subsets: ["latin"],
});
const geistMono = Geist_Mono({
variable: "--font-geist-mono",
subsets: ["latin"],
});
export const metadata: Metadata = {
title: 'My Neon App',
description: 'A Next.js application with Neon Auth',
};
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
// [!code ++]
// [!code ++]
// [!code ++]
// [!code ++]
{children} // [!code ++]
// [!code ++]
);
}
```
Import the Neon Auth UI styles in your `app/globals.css` file. Add this line at the top of the file:
```css
@import "tailwindcss";
@import "@neondatabase/neon-js/ui/css"; // [!code ++]
```
Now that the Auth provider and styles are set up, let’s build the pages for signing up and signing in
Create a dynamic route segment for authentication and account views in `app/auth/[path]/page.tsx` and `app/account/[path]/page.tsx` respectively.
- `AuthView` - with dynamic route segment covers the following paths:
- `/auth/sign-in` - Sign in with email/password and social providers
- `/auth/sign-up` New account registration
- `/auth/sign-out` Sign the user out of the applications
- `AccountView` - with dynamic route segment covers the following paths:
- `/account/settings` - User can manage their profile details
- `/account/security` - Change password and list active session
Create a new page in `app/auth/[path]/page.tsx` and copy-paste following code:
```tsx
import { AuthView } from '@neondatabase/neon-js/auth/react/ui';
export const dynamicParams = false;
export default async function AuthPage({ params }: { params: Promise<{ path: string }> }) {
const { path } = await params;
return (
);
}
```
Create a new page in `app/account/[path]/page.tsx` and copy-paste following code:
```tsx
import { AccountView } from '@neondatabase/neon-js/auth/react/ui';
import { accountViewPaths } from '@neondatabase/neon-js/auth/react/ui/server';
export const dynamicParams = false;
export function generateStaticParams() {
return Object.values(accountViewPaths).map((path) => ({ path }));
}
export default async function AccountPage({ params }: { params: Promise<{ path: string }> }) {
const { path } = await params;
return (
);
}
```
Start the development server, and then open http://localhost:3000
```bash
npm run dev
```
## Next steps
- [Add email verification](/docs/auth/guides/email-verification)
- [Learn how to branch your auth](/docs/auth/branching-authentication)