This project is a user management API built with Node.js and Express. It provides basic functionalities like user registration, login, updating user details, password management, and user deletion. The API uses JWT authentication to secure routes.
- User registration and login with JWT-based authentication
- Protected routes using JWT middleware
- Password hashing using bcrypt
- CRUD operations for user management
- Node.js
- Express
- JWT (JSON Web Token)
- bcrypt
- Neon database (PostgreSQL)
- Helmet for security
- CORS for cross-origin requests
- Clone the repository:
git clone <repository-url>
- Install dependencies:
npm install
- Setup .env file with your database credentials and JWT secret key:
JWT_SECRET=<your-secret-key> DATABASE_URL=<your-database-url>
- Start the server:
npm start
- Endpoint:
/api/users/register - Method:
POST - Description: Register a new user.
- Request Body:
{ "username": "john_doe", "email": "john@example.com", "password": "password123" }- Response:
{ "user_id": 5, "username": "john_doe", "email": "john@example.com", "passwordhash": "hashed_password" }
- Endpoint:
/api/users/login - Method:
POST - Description: Authenticate and log in a user.
- Request Body:
{ "email": "john@example.com", "password": "password123" }- Response:
{ "user": { "user_id": 5, "username": "john_doe", "email": "john@example.com" }, "token": "token_value" }
- Endpoint:
/api/users - Method:
GET - Headers:
{
"Authorization": "Bearer <token>"
}- Description: Get a list of all users.
- Response:
[
{
"user_id": 5,
"username": "john_doe",
"email": "john@example.com"
},
{
"user_id": 6,
"username": "jane_doe",
"email": "jane@example.com"
}
]- Endpoint:
/api/users/:id - Method:
GET - Headers:
{
"Authorization": "Bearer <token>"
}- Description: Get a user by their ID.
- Response:
{
"user_id": 5,
"username": "john_doe",
"email": "john@example.com"
}- Endpoint:
/api/users/:id - Method:
PUT - Request Body:
{
"username": "john_doe",
"email": "john@example.com",
"password": "new_password" // Optional
}- Headers:
{
"Authorization": "Bearer <token>"
}- Description: Update user by ID.
- Response:
{
"user_id": 5,
"username": "john_doe",
"email": "john@example.com"
}- Endpoint:
/api/users/:id/password - Method:
PUT - Request Body:
{
"new_password": "new_password"
}- Headers:
{
"Authorization": "Bearer <token>"
}- Description: Update user password by ID.
- Response:
{
"user_id": 5,
"username": "john_doe",
"email": "john@example.com"
}- Endpoint:
/api/users/:id - Method:
DELETE - Headers:
{
"Authorization": "Bearer <token>"
}- Description: Delete a user by ID.
- **Response
{
"user_id": 5,
"username": "john_doe",
"email": "john@example.com"
}- authMiddleware.js: Middleware for JWT authentication.
- Description: The API returns appropriate status codes and error messages for various scenarios.
- 200 OK: Request was successful
- 400 Bad Request: Invalid input or data
- 401 Unauthorized: Missing or invalid JWT token
- 403 Forbidden: Access denied due to missing token
- 404 Not Found: User not found
- 500 Internal Server Error: Server error while processing the request • 500 Internal Server Error: Server error while processing the request