---
title: Use Neon Auth with TanStack Router
subtitle: Set up authentication using pre-built UI components
enableTableOfContents: true
updatedOn: '2025-12-15T17:10:57.641Z'
layout: wide
---
If you don't have a Neon project yet, create one at [console.neon.tech](https://console.neon.tech).
Go to the **Auth** page in your project dashboard and click **Enable Auth**.
You can then find your Auth URL on the Configuration tab. Copy this URL - you'll need it in the next step.

Create a new TanStack Router app using the file-router template.
```bash
npx create-tsrouter-app@latest my-app --template file-router --tailwind
```
Install the Neon Auth SDK and UI library:
```bash
cd my-app && 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
VITE_NEON_AUTH_URL=https://ep-xxx.neonauth.us-east-1.aws.neon.tech/neondb/auth
```
Open your existing `src/styles.css` file and add this import at the **top**, right after the Tailwind import:
```css
@import '@neondatabase/neon-js/ui/css';
```
Create a `src/auth.ts` file to initialize the auth client:
```typescript
import { createAuthClient } from '@neondatabase/neon-js/auth';
import { BetterAuthReactAdapter } from '@neondatabase/neon-js/auth/react';
export const authClient = createAuthClient(import.meta.env.VITE_NEON_AUTH_URL, { adapter: BetterAuthReactAdapter() });
```
Wrap your application with the `NeonAuthUIProvider` in `src/routes/__root.tsx`. This makes the auth state available to the UI components used throughout your app.
Pass props to `NeonAuthUIProvider` for any features you want to use. Only the `authClient` prop is required.
Example: Adding optional props
```tsx
{children}
```
```tsx {4-5,9,22}
import { Outlet, createRootRoute } from '@tanstack/react-router';
import { TanStackRouterDevtoolsPanel } from '@tanstack/react-router-devtools';
import { TanStackDevtools } from '@tanstack/react-devtools';
import { NeonAuthUIProvider } from '@neondatabase/neon-js/auth/react';
import { authClient } from '../auth';
export const Route = createRootRoute({
component: () => (
,
},
]}
/>
),
});
```
Create a route to handle authentication views (sign in, sign up, etc.). Create `src/routes/auth.$pathname.tsx`:
```tsx
import { createFileRoute } from '@tanstack/react-router';
import { AuthView } from '@neondatabase/neon-js/auth/react/ui';
export const Route = createFileRoute('/auth/$pathname')({
component: Auth,
});
function Auth() {
const { pathname } = Route.useParams();
return (
);
}
```
Create a route to handle account management views. Create `src/routes/account.$pathname.tsx`:
```tsx
import { createFileRoute } from '@tanstack/react-router';
import { AccountView } from '@neondatabase/neon-js/auth/react/ui';
export const Route = createFileRoute('/account/$pathname')({
component: Account,
});
function Account() {
const { pathname } = Route.useParams();
return (
);
}
```
You can protect your routes using the `SignedIn` and `RedirectToSignIn` components. Access the user's session and profile data using the `useSession` hook.
Update `src/routes/index.tsx` to protect the home page:
```tsx
import { createFileRoute } from '@tanstack/react-router';
import { SignedIn, UserButton, RedirectToSignIn } from '@neondatabase/neon-js/auth/react/ui';
import { authClient } from '@/auth';
export const Route = createFileRoute('/')({
component: Home,
});
function Home() {
const { data } = authClient.useSession();
return (
<>
>
);
}
```
Start the development server, then open [http://localhost:3000](http://localhost:3000). You'll be redirected to the sign-in page.
```bash
npm run dev
```
As users sign up, their profiles are stored in your Neon database in the `neon_auth.user` table.
Query your users table in the SQL Editor to see your new users:
```sql
SELECT * FROM neon_auth.user;
```
## Next steps
- [Add email verification](/docs/auth/guides/email-verification)
- [Learn how to branch your auth](/docs/auth/branching-authentication)