Skip to content

emanuelefavero/next-auth-prisma-db

Repository files navigation

NextAuth.js Prisma Database

This is a starter project using NextAuth.js as an authentication provider and Prisma as a database ORM for PostgreSQL.

It allows to store user credentials automatically in a database during the authentication process

IMPORTANT

When you are deploying your application to production, remember to set Homepage URL and Authorization callback URL in your GitHub OAuth App settings in Developer Settings on GitHub.

Do the same for other providers

Example

  • Homepage URL: https://yourdomain.com
  • Authorization callback URL: https://yourdomain.com/api/auth/callback/github

Screenshot

screenshot

Getting Started

  • clone this repo and cd into it
  • run npm install
  • Add .env file to the root of the project with the following content:
# * Prisma
# You can create a PostgreSQL database easly on Supabase
DATABASE_URL="postgresql://YOUR_SUPABASE_URL/postgres"

> Note: After 2023 Supabase migration, we need to copy/paste a new DATABASE_URL from Supabase. This will probably not work


# * NextAuth.js
# You will find these values in your GitHub OAuth App settings in Developer Settings on GitHub (Client ID and client secret), you will need to create a new OAuth App (remember to set Homepage URL and Authorization callback URL to production URL during production)
GITHUB_ID=YOUR_GITHUB_ID
GITHUB_SECRET=YOUR GITHUB SECRET

# ! Your NEXTAUTH_SECRET secret should be at least 32 characters long, not parakeet :)
NEXTAUTH_SECRET=parakeet
# ! Your NEXTAUTH_URL should be the URL of your site, change it in production
NEXTAUTH_URL=http://localhost:3000

Note: If you are deploying to Vercel, the NEXTAUTH_URL env variable is not necessary

Running the app

npm run dev

Note: NextAuth.js has many providers, you can find them here

Add Prisma to an existing NextAuth.js project

Prisma Configuration for this project

  • run npx prisma migrate dev every time you change the schema
  • run npx prisma studio to if you want to see the database in a GUI

Prisma Configuration for new projects

  • run npm install prisma --save-dev

  • run npm install next-auth @prisma/client @next-auth/prisma-adapter

  • run npx prisma init to initialize the Prisma project

Note: I've created a new database with Supabase which is a firebase-like database service that uses PostgreSQL

  • Create a new database with Supabase

  • Go to Project Settings / Database / Connection string / URI and copy the URI string

  • Paste the URI string in the .env file (DATABASE_URL)

  • run npx db pull if you already have data in your database and you want to generate the Prisma schema

  • add your schema in schema.prisma

  • run npx prisma migrate dev --name init to create the database schema (run npx prisma migrate dev every time you change the schema)

  • run npx prisma generate to generate the Prisma Client (run this command every time you change the schema)

  • run npx prisma studio to if you want to see the database in a GUI

NextAuth.js Providers

Learn More

License

Releases

No releases published

Packages

No packages published