This repository provides an example authentication system for Next.js using NextAuth.js. It includes user authentication with providers such as Google, GitHub, and credentials-based login, utilizing MongoDB as the database.
- Introduction
- Features
- Prerequisites
- Installation
- Environment Variables Setup
- Database Setup
- Running the Project
- Contributing
- License
This project demonstrates how to integrate Next.js with NextAuth.js for a fully functional authentication system. It supports multiple providers and stores user data in a MongoDB database using Prisma as an ORM.
- Authentication with Google, GitHub, and credentials
- Session management using NextAuth.js
- MongoDB as a persistent database via Prisma ORM
- Secure environment with
.env
configuration
Before you begin, ensure you have the following installed on your system:
- Node.js
- PNPM (for package management)
- MongoDB (for database setup)
- Prisma CLI (for database schema management)
Follow these steps to install and set up the project:
-
Clone the repository:
git clone https://github.com/Ikuzweshema/next-auth-example.git
-
Navigate to the project directory:
cd next-auth-example
- Install dependencies using PNPM:
pnpm install
Next, you need to set up the environment variables for the project. Create a .env
file in the root directory and configure the following variables:
NEXTAUTH_SECRET=your-nextauth-secret
GOOGLE_CLIENT_ID=your-google-client-id
GOOGLE_CLIENT_SECRET=your-google-client-secret
GITHUB_ID=your-github-id
GITHUB_SECRET=your-github-secret
DATABASE_URL=mongodb+srv://username:password@cluster.mongodb.net/dbname
Ensure you replace the placeholder values with your actual credentials.
The project uses Prisma to manage the database schema. To set up the MongoDB database:
-
Create a MongoDB database on MongoDB Atlas or any other MongoDB provider.
-
Push the Prisma schema to the database using this command:
pnpm prisma db push
This will synchronize your Prisma schema with your MongoDB database.
Once you have the environment variables and database set up, you can start the development server:
pnpm dev
The application will be running locally on http://localhost:3000 .
Contributions are welcome! If you would like to contribute to this project, feel free to submit a pull request. Please ensure your contributions follow the project's coding standards.
This project is licensed under the MIT License. See the LICENSE file for more details.