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

https://github.com/berkanjs/auth-tutorial-node.js-express-mongodb-swagger

This project is a Node.js application that covers basic authentication operations like user registration, login, email verification, password reset, etc. It also provides API documentation with Swagger.
https://github.com/berkanjs/auth-tutorial-node.js-express-mongodb-swagger

Last synced: 8 months ago
JSON representation

This project is a Node.js application that covers basic authentication operations like user registration, login, email verification, password reset, etc. It also provides API documentation with Swagger.

Awesome Lists containing this project

README

          

# ๐Ÿ” Auth Tutorial โ€“ Node.js, Express, MongoDB & Swagger

This project is a Node.js-based authentication system that handles user registration, login, email verification, password reset, and more. It also includes Swagger API documentation.

## ๐Ÿ“ Project Structure

```
auth-tutorial/
โ”œโ”€โ”€ backend/
โ”‚ โ”œโ”€โ”€ controllers/
โ”‚ โ”œโ”€โ”€ routes/
โ”‚ โ”œโ”€โ”€ models/
โ”‚ โ”œโ”€โ”€ middleware/
โ”‚ โ”œโ”€โ”€ utils/
โ”‚ โ”œโ”€โ”€ mailtrap/
โ”‚ โ”œโ”€โ”€ swagger.yaml
โ”‚ โ””โ”€โ”€ server.js
โ”œโ”€โ”€ frontend/
โ”œโ”€โ”€ node_modules/
โ”œโ”€โ”€ .env
โ”œโ”€โ”€ package.json
โ””โ”€โ”€ README.md
```

## ๐Ÿš€ Setup

### 1. Clone the Repository

```bash
git clone https://github.com/your-username/auth-tutorial.git
cd auth-tutorial
```

### 2. Install Dependencies

```bash
npm install
```

### 3. Configure Environment Variables

Create a `.env` file in the root directory and add the following:

```
PORT=5000
MONGO_URI=mongodb+srv://user:password@cluster.mongodb.net/dbname
JWT_SECRET=your_jwt_secret
CLIENT_URL=http://localhost:5173
```

### 4. Start the App

```bash
npm start
```

## ๐Ÿ“ฌ Auth Endpoints

- โœ… Sign Up: `POST /api/auth/signup`
- โœ… Login: `POST /api/auth/login`
- โœ… Logout: `POST /api/auth/logout`
- โœ… Verify Email: `POST /api/auth/verify-email`
- โœ… Forgot Password: `POST /api/auth/forgot-password`
- โœ… Reset Password: `POST /api/auth/reset-password/:token`
- โœ… Check Auth: `GET /api/auth/check-auth`

## ๐Ÿ“˜ Swagger API Documentation

Access Swagger UI at:

```bash
http://localhost:5000/api-docs
```

Swagger schema file: `backend/swagger.yaml`

## โœ‰๏ธ Email Service

This project uses [Mailtrap](https://mailtrap.io/) for testing email functionalities. Email credentials are stored in `.env`. These actions are email-supported:

- Email verification
- Sending password reset link
- Welcome email
- Password reset success message

## โš ๏ธ Security Notes

- Passwords are hashed with `bcryptjs`.
- Authentication is managed via JWT.
- Cookies are used for secure session handling (HTTPOnly).
- Token expiration is implemented for enhanced security.

## ๐Ÿ‘จโ€๐Ÿ’ป Tech Stack

- Node.js
- Express.js
- MongoDB (Mongoose)
- JWT (JSON Web Tokens)
- Swagger
- Mailtrap
- BcryptJS

## ๐Ÿงช Sample Request Bodies

### Sign Up

```json
{
"email": "test@example.com",
"name": "Berkan",
"password": "secret123"
}
```

### Login

```json
{
"email": "test@example.com",
"password": "secret123"
}
```

### Verify Email

```json
{
"code": "123456"
}
```

### Forgot Password

```json
{
"email": "test@example.com"
}
```

### Reset Password

```json
{
"password": "newPassword123"
}
```

## ๐Ÿงก Contributing

Pull requests are welcome! Feel free to report bugs, suggest improvements, or contribute to the project.

---

**Developed by BerkanJS**
To integrate with frontend, don't forget to set the `CLIENT_URL` in your `.env`:

```bash
CLIENT_URL=http://localhost:5173
```