/nextjs-auth

Next.js authentication boilerplate

Primary LanguageJavaScript

Next.js Authentication Boilerplate

Installation

  • Install dependencies:
npm i
  • Create a MongoDB cluster and get the connection string from the MongoDB dashboard.

  • Create a .env file in the root directory as per the .env.example file:

cp .env.example .env
  • Set MongoDB credentials in the .env file:
MONGODB_USER="user"
MONGODB_PASSWORD="password"
MONGODB_DATABASE="database"
MONGODB_CLUSTER="cluster0"
MONGODB_DOMAIN="mzcsh.mongodb.net"
  • Generate a secret key:
openssl rand -base64 32
  • Set AUTH_SECRET and NEXTAUTH_URL in the .env file:
NEXTAUTH_SECRET="<generated_key>"
NEXTAUTH_URL="http://localhost:3000"
  • Set the ADMIN_EMAIL in the .env file (GitHub email used in this example):
ADMIN_EMAIL="admin@gmail.com"
  • Create a GitHub OAuth app and set the GITHUB_ID and GITHUB_SECRET in the .env file:
GITHUB_ID="<github_id>"
GITHUB_SECRET="<github_secret>"
  • Run the server:
npm run dev
# or
npm start

Resources

Author