Skip to content

SkillForge-ORG/nestjs_with_mongodb

Repository files navigation

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads CircleCI Coverage Discord Backers on Open Collective Sponsors on Open Collective Donate us Support us Follow us on Twitter

NestJS MongoDB Authentication API

A robust authentication system built with NestJS, MongoDB, and JWT, featuring user management and post creation capabilities. This project was developed using Windsurf, the world's first agentic IDE.

Features

  • User Authentication

    • JWT-based authentication
    • Secure password hashing with bcrypt
    • Login and registration endpoints
    • Protected routes
  • User Management

    • Create, read, update, and delete users
    • Email validation
    • Password validation
  • Post Management

    • Create, read, update, and delete posts
    • Posts associated with users
    • Protected post endpoints

Prerequisites

  • Node.js (v14 or later)
  • MongoDB database
  • npm or yarn package manager

Environment Variables

Create a .env file in the root directory with the following variables:

DATABASE_URL="mongodb+srv://your-username:your-password@your-cluster-url/your-database?retryWrites=true&w=majority"
JWT_SECRET="your-jwt-secret"
PORT=3333

Installation

# Install dependencies
$ npm install

# Push database schema
$ npx prisma db push

# Generate Prisma client
$ npx prisma generate

Running the app

# development
$ npm run start

# watch mode
$ npm run start:dev

# production mode
$ npm run start:prod

API Documentation

Once the application is running, visit http://localhost:3333/api to access the Swagger documentation.

Authentication Endpoints

  • POST /auth/register - Register a new user
  • POST /auth/login - Login and receive JWT token

Protected Endpoints (Requires JWT Token)

Users:

  • GET /users - Get all users
  • GET /users/:id - Get user by ID
  • PATCH /users/:id - Update user
  • DELETE /users/:id - Delete user

Posts:

  • GET /posts - Get all posts
  • GET /posts/:id - Get post by ID
  • POST /posts - Create new post
  • PATCH /posts/:id - Update post
  • DELETE /posts/:id - Delete post

Security Features

  • Password hashing using bcrypt
  • JWT token authentication
  • Protected routes with Guards
  • Input validation using class-validator
  • Swagger API documentation with bearer auth

Project Structure

src/
├── auth/              # Authentication module
├── users/             # Users module
├── posts/             # Posts module
├── prisma/            # Prisma schema and migrations
└── main.ts            # Application entry point

Testing

# unit tests
$ npm run test

# e2e tests
$ npm run test:e2e

# test coverage
$ npm run test:cov

License

This project is MIT licensed.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published