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.
- Host: GitHub
- URL: https://github.com/berkanjs/auth-tutorial-node.js-express-mongodb-swagger
- Owner: BerkanJs
- Created: 2025-04-17T14:16:06.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-17T14:20:19.000Z (about 1 year ago)
- Last Synced: 2025-05-18T12:09:25.693Z (about 1 year ago)
- Language: JavaScript
- Size: 6.57 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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
```