A quick start Next.js template demonstrating secure user authentication and authorization using Neon RLS Authorize with Stack Auth integration. This guide primarily uses SQL from the backend to enforce row-level security policies.
- Next.js application with TypeScript
- User authentication powered by Stack Auth
- Row-level security using Neon RLS Authorize
- Database migrations with Drizzle ORM
- Ready-to-deploy configuration for Vercel, Netlify, and Render
- Neon account with a new project
- Stack Auth account with a new project
- Node.js 18+ installed locally
Deploy directly to your preferred hosting platform:
Make sure to add your website URL as a Trusted Domain on your Stack Auth project settings.
-
Sign up for a Stack Auth account and create a new project.
-
Navigate to the project settings and create an API key.
-
Upon creating the API key, you will receive
NEXT_PUBLIC_STACK_PROJECT_ID
,NEXT_PUBLIC_STACK_PUBLISHABLE_CLIENT_KEY
andSTACK_SECRET_SERVER_KEY
. Keep these handy for the next steps.
-
Open your Neon Console and click "RLS Authorize" in your project's settings
-
Add a new authentication provider
-
Set the JWKS URL to:
https://api.stack-auth.com/api/v1/projects/<project-id>/.well-known/jwks.json
Replace
<project-id>
with your Stack Auth project ID
-
Clone the repository:
git clone https://github.com/neondatabase-labs/stack-nextjs-neon-rls-authorize cd stack-nextjs-neon-rls-authorize
-
Install dependencies:
npm install
-
Create
.env
file with the following variables:NEXT_PUBLIC_STACK_PROJECT_ID= NEXT_PUBLIC_STACK_PUBLISHABLE_CLIENT_KEY= STACK_SECRET_SERVER_KEY= # Database connections DATABASE_URL= # neondb_owner role connection DATABASE_AUTHENTICATED_URL= # authenticated role connection
Get your Stack Auth keys from your Stack Auth project dashboard.
-
Set up the database:
npm run drizzle:generate # Generate migrations npm run drizzle:migrate # Apply migrations
-
Start the development server:
npm run dev
-
Visit
http://localhost:3000
to see the application running.
- Upgrade your Stack Auth project to production mode by navigating to the project settings.
- Verify that the JWKS URL in your Neon RLS Authorize configuration is correctly pointing to your Stack Auth project.
- Neon RLS Authorize Tutorial
- Simplify RLS with Drizzle
- Stack Auth Documentation
- Neon RLS Authorize + Stack Auth Integration
Contributions are welcome! Please feel free to submit a Pull Request.