This API is designed to handle basic user management operations for any application. It provides endpoints to register new users, log them in, and allow them to view their profile. It ensures that passwords are securely stored, users' data is protected, and only authenticated users can access their profile.
- User Registration: Register a new user by providing a username, email, and password.
- User Login: Log in an existing user and generate a JSON Web Token (JWT) for session management.
- User Profile: Allow authenticated users to view their profile details.
- User Profile Edit: Allow authenticated users to edit their profile details.
- User Profile Delete: Allow authenticated users to Delete their profile.
-
User Registration:
- Accepts
username,email, andpasswordfrom the user. - Checks if the email already exists in the database.
- If unique, it hashes the password using
bcryptand saves the user details in the database. - Returns a success message and user ID.
- Accepts
-
User Login:
- Accepts
emailandpasswordfor login. - Verifies if the user exists and if the provided password matches the hashed password in the database.
- If authentication is successful, it generates a JWT and returns it in the response.
- Accepts
-
User Profile:
- Protected route that requires a valid JWT.
- Allows authenticated users to access their own profile information.
-
User Profile Edit:
- Protected route that requires a valid JWT.
- Allows authenticated users to edit their own profile information.
-
User Profile Delete:
- Protected route that requires a valid JWT.
- Allows authenticated users to delete their own profile.
- express: Web framework for building the API endpoints.
- mongoose: MongoDB ODM (Object Data Modeling) library for database management.
- bcrypt: Library for hashing passwords securely.
- jsonwebtoken: Used to generate and verify JWTs for user authentication.
- dotenv: Manages environment variables from a
.envfile. - nodemon: Development tool that automatically restarts the server on code changes.
- Node.js and npm installed.
- MongoDB running locally or remotely.
-
Clone the repository:
git clone https://github.com/IAmKushagraSharma/user-management-express-api.git cd user-management-api -
Install dependencies:
npm install
-
Set up the environment variables: Create a
.envfile in the root directory of your project and add the following variables:MONGO_URI=your_mongodb_connection_string JWT_SECRET=your_jwt_secret_key
-
Start the server: To run the server, use the following command:
npm run dev
This will start the server with nodemon in development mode. The server will auto-reload when any changes are made.
-
API Endpoints:
-
User Registration:
-
POST /api/users/register -
Request body:
{ "username": "your_username", "email": "your_email@example.com", "password": "your_password" }
-
-
User Login:
-
POST /api/users/login -
Request body:
{ "email": "your_email@example.com", "password": "your_password" }
-
-
User Profile:
GET /api/users/profile- Authorization: Bearer token (JWT)
-
You can look at more details at
https://localhost:3000when you run it locally.
.
├── app.js # Main app initialization
├── config
│ └── db.js # MongoDB connection configuration
├── controllers
│ └── userController.js # User registration, login, and profile logic
├── middlewares
│ └── authMiddleware.js # JWT authentication middleware
├── models
│ └── userModel.js # User schema model
├── routes
│ └── userRoutes.js # API route definitions
├── public
│ └── index.html # API documentation page
├── server.js # Server startup
├── .env # Environment variables
├── package.json # Dependencies and scripts
└── README.md # API documentation
The API uses two main environment variables:
- MONGO_URI: The connection string for MongoDB.
- JWT_SECRET: A secret key used for signing JWTs.
These variables should be stored in a .env file in the root directory.
- The API uses
bcryptto hash user passwords before storing them in the database. - When logging in, the provided password is compared with the hashed password stored in the database to ensure security.
- JWT (JSON Web Token) is used to manage user sessions.
- When a user logs in, the API generates a JWT that contains the user's ID. This token is returned to the client.
- For accessing protected routes (like the profile page), the client must send this token in the
Authorizationheader (Bearer token).
This API provides a secure and efficient way to manage user authentication and profiles. It's built with simplicity and security in mind, making it suitable for most web applications.