Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ikuzweshema/next-auth-example

🔐Example of Multi Factor Authentication in next Js with Next-auth
https://github.com/ikuzweshema/next-auth-example

2factor-authentication app-router next-auth nextjs oauth prisma-orm shadcn-ui typescript zod

Last synced: 2 months ago
JSON representation

🔐Example of Multi Factor Authentication in next Js with Next-auth

Awesome Lists containing this project

README

        

# Next.js Authentication Example with NextAuth.js

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.

## Table of Contents

- [Introduction](#introduction)
- [Features](#features)
- [Prerequisites](#prerequisites)
- [Installation](#installation)
- [Environment Variables Setup](#environment-variables-setup)
- [Database Setup](#database-setup)
- [Running the Project](#running-the-project)
- [Contributing](#contributing)
- [License](#license)

## Introduction

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.

## Features

- Authentication with Google, GitHub, and credentials
- Session management using **NextAuth.js**
- MongoDB as a persistent database via **Prisma ORM**
- Secure environment with `.env` configuration

## Prerequisites

Before you begin, ensure you have the following installed on your system:

- [Node.js](https://nodejs.org/)
- [PNPM](https://pnpm.io/) (for package management)
- [MongoDB](https://www.mongodb.com/) (for database setup)
- [Prisma CLI](https://www.prisma.io/docs/concepts/components/prisma-cli) (for database schema management)

## Installation

Follow these steps to install and set up the project:

1. **Clone the repository:**

```bash
git clone https://github.com/Ikuzweshema/next-auth-example.git
```

2. **Navigate to the project directory:**

```bash
cd next-auth-example
````

3. **Install dependencies using PNPM:**

```bash
pnpm install
```

## Environment Variables Setup

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:

# .env file

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:[email protected]/dbname

Ensure you replace the placeholder values with your actual credentials.

## Database Setup

The project uses **Prisma** to manage the database schema. To set up the MongoDB database:

1. **Create a MongoDB database** on [MongoDB Atlas](https://www.mongodb.com/cloud/atlas) or any other MongoDB provider.

2. Push the Prisma schema to the database using this command:

```bash
pnpm prisma db push
```

This will synchronize your Prisma schema with your MongoDB database.

## Running the Project

Once you have the environment variables and database set up, you can start the development server:

```bash
pnpm dev
```

The application will be running locally on [http://localhost:3000](http://localhost:3000) .

## Contributing

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.

## License

This project is licensed under the **MIT License**. See the [LICENSE](LICENSE) file for more details.