Code Monkey home page Code Monkey logo

basic-rest-api's Introduction

User Authentication REST API with CRUD Features

This repository contains a fundamental RESTful API for user authentication and management. Built using Node.js, Express, MongoDB, TypeScript, and Lodash, it offers essential endpoints for creating, updating, retrieving, and deleting user profiles.

Table of Contents

Key Features:

  • User Authentication: Secure user authentication mechanisms are implemented.
  • CRUD Operations: Supports Create, Read, Update, and Delete operations for user profiles.
  • Simplicity: Focuses on core functionalities without unnecessary complexity.
  • TypeScript: Codebase is written in TypeScript for code quality and maintainability.
  • MongoDB: User data is stored and managed in a MongoDB database.
  • Lodash: Efficient data manipulation and processing using Lodash.

Getting Started:

To use this REST API, follow these steps:

Setting Up Environment Variables:

  1. Create a .env file in the root directory.
  2. Set the following environment variables with your values (replace with actual values for production):

PORT= MONGODB_URI="" SECRET=""

For setting up the MONGODB_URI variable, you can follow the MongoDB documentation on creating a cluster and obtaining the connection URI here.

API Routes:

  • POST /auth/register: User registration.
  • POST /auth/login: User login.
  • GET /users (protected): Retrieve all users.
  • DELETE /users/:id (protected): Delete a user by ID.
  • PATCH /users/:id (protected): Update a user by ID.

Database Model:

  • User model includes fields for username, email, and an authentication object containing password, salt, and sessionToken.
  • This model is used for CRUD operations on user data.

Testing with Postman:

You can use Postman or any other frontend helper to test the API. Here's how to structure payloads for key endpoints:

  • User Registration (POST /auth/register): JSON payload with username, email, and authentication.password.
  • User Login (POST /auth/login): JSON payload with email and authentication.password.
  • Get All Users (GET /users): No payload required. Include a valid JWT in the Authorization header.
  • Delete User (DELETE /users/:id): No payload required. Include a valid JWT in the Authorization header.
  • Update User (PATCH /users/:id): JSON payload with the fields you want to update (e.g., username, email, or authentication.password). Include a valid JWT in the Authorization header.

Running the API:

Start the API by running npm start.

Important Note:

This repository is intended for educational and developmental purposes. It serves as a solid foundation for user authentication and CRUD operations and is ideal for kickstarting development, building prototypes, or testing projects. However, it is not recommended for production use without further enhancements:

  • Security: Implement additional security measures and best practices to safeguard user data.
  • Performance: Optimize code and infrastructure for efficient handling of production loads.
  • Scalability: Evaluate the application's scalability to meet the demands of a larger user base.
  • Testing: Conduct comprehensive testing and quality assurance to identify and resolve potential issues.

In summary, while this repository provides a valuable starting point, exercise caution and diligence when transitioning to production environments.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.