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.
- 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.
To use this REST API, follow these steps:
- Create a
.envfile in the root directory. - 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.
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.
- User model includes fields for
username,email, and anauthenticationobject containingpassword,salt, andsessionToken. - This model is used for CRUD operations on user data.
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 withusername,email, andauthentication.password. - User Login (POST
/auth/login): JSON payload withemailandauthentication.password. - Get All Users (GET
/users): No payload required. Include a valid JWT in theAuthorizationheader. - Delete User (DELETE
/users/:id): No payload required. Include a valid JWT in theAuthorizationheader. - Update User (PATCH
/users/:id): JSON payload with the fields you want to update (e.g.,username,email, orauthentication.password). Include a valid JWT in theAuthorizationheader.
Start the API by running npm start.
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.