Get started with Next.js, Prisma Postgres, and Auth.js
This repository provides a boilerplate to quickly set up a Next.js demo application with authentication using NextAuth.js v4, Prisma Postgres and Prisma ORM, and deploy it to Vercel. It includes an easy setup process and example routes that demonstrate basic CRUD operations against the database.
After cloning the repo and navigating into it, install dependencies:
npm install
Create a Prisma Postgres instance by running the following command:
npx prisma init --db
This command is interactive and will prompt you to:
Once the command has terminated, copy the Database URL from the terminal output. You'll need it in the next step when you configure your .env
file.
.env
fileYou now need to configure your database connection via an environment variable.
First, create an .env
file:
touch .env
Then update the .env
file by replacing the existing DATABASE_URL
value with the one you previously copied. It will look similar to this:
DATABASE_URL="prisma+postgres://accelerate.prisma-data.net/?api_key=PRISMA_POSTGRES_API_KEY"
To ensure your authentication works properly, you'll also need to set env vars for NextAuth.js:
AUTH_SECRET="RANDOM_32_CHARACTER_STRING"
You can generate a random 32 character string for the AUTH_SECRET
secret with this command:
npx auth secret
In the end, your entire .env
file should look similar to this (but using your own values for the env vars):
DATABASE_URL="prisma+postgres://accelerate.prisma-data.net/?api_key=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcGlfa2V5IjoiMWEzMjBiYTEtYjg2Yy00ZTA5LThmZTktZDBhODA3YjQwZjBkIiwidGVuYW50X2lkIjoiY2RhYmM3ZTU1NzdmMmIxMmM0ZTI1Y2IwNWJhZmZhZmU4NjAxNzkxZThlMzhlYjI1NDgwNmIzZjI5NmU1NTkzNiIsImludGVybmFsX3NlY3JldCI6ImI3YmQzMjFhLTY2ODQtNGRiMC05ZWRiLWIyMGE2ZTQ0ZDMwMSJ9.JgKXQBatjjh7GIG3_fRHDnia6bDv8BdwvaX5F-XdBfw"AUTH_SECRET="gTwLSXFeNWFRpUTmxlRniOfegXYw445pd0k6JqXd7Ag="
Run the following commands to set up your database and Prisma schema:
npx prisma migrate dev --name init
Add initial data to your database:
npx prisma db seed
# Using yarnyarn prisma db seed# Using pnpmpnpm prisma db seed# Using bunbun prisma db seed
Start the development server:
npm run dev
# Using yarnyarn dev# Using pnpmpnpm run dev# Using bunbun run dev
Once the server is running, visit http://localhost:3000
to start using the app.
Get started with Next.js, Prisma Postgres, and Auth.js
This repository provides a boilerplate to quickly set up a Next.js demo application with authentication using NextAuth.js v4, Prisma Postgres and Prisma ORM, and deploy it to Vercel. It includes an easy setup process and example routes that demonstrate basic CRUD operations against the database.
After cloning the repo and navigating into it, install dependencies:
npm install
Create a Prisma Postgres instance by running the following command:
npx prisma init --db
This command is interactive and will prompt you to:
Once the command has terminated, copy the Database URL from the terminal output. You'll need it in the next step when you configure your .env
file.
.env
fileYou now need to configure your database connection via an environment variable.
First, create an .env
file:
touch .env
Then update the .env
file by replacing the existing DATABASE_URL
value with the one you previously copied. It will look similar to this:
DATABASE_URL="prisma+postgres://accelerate.prisma-data.net/?api_key=PRISMA_POSTGRES_API_KEY"
To ensure your authentication works properly, you'll also need to set env vars for NextAuth.js:
AUTH_SECRET="RANDOM_32_CHARACTER_STRING"
You can generate a random 32 character string for the AUTH_SECRET
secret with this command:
npx auth secret
In the end, your entire .env
file should look similar to this (but using your own values for the env vars):
DATABASE_URL="prisma+postgres://accelerate.prisma-data.net/?api_key=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcGlfa2V5IjoiMWEzMjBiYTEtYjg2Yy00ZTA5LThmZTktZDBhODA3YjQwZjBkIiwidGVuYW50X2lkIjoiY2RhYmM3ZTU1NzdmMmIxMmM0ZTI1Y2IwNWJhZmZhZmU4NjAxNzkxZThlMzhlYjI1NDgwNmIzZjI5NmU1NTkzNiIsImludGVybmFsX3NlY3JldCI6ImI3YmQzMjFhLTY2ODQtNGRiMC05ZWRiLWIyMGE2ZTQ0ZDMwMSJ9.JgKXQBatjjh7GIG3_fRHDnia6bDv8BdwvaX5F-XdBfw"AUTH_SECRET="gTwLSXFeNWFRpUTmxlRniOfegXYw445pd0k6JqXd7Ag="
Run the following commands to set up your database and Prisma schema:
npx prisma migrate dev --name init
Add initial data to your database:
npx prisma db seed
# Using yarnyarn prisma db seed# Using pnpmpnpm prisma db seed# Using bunbun prisma db seed
Start the development server:
npm run dev
# Using yarnyarn dev# Using pnpmpnpm run dev# Using bunbun run dev
Once the server is running, visit http://localhost:3000
to start using the app.