This is a Node.js-based RESTful API for managing user accounts, including user registration, login, and managing user-specific data such as favourites and history. The API uses MongoDB for data storage and JWT for authentication.
- User registration with password hashing
- User login with JWT authentication
- Manage user favourites (add, remove, retrieve)
- Manage user history (add, remove, retrieve)
- Secure API endpoints with Passport.js and JWT
- Node.js: Backend runtime
- Express: Web framework
- MongoDB: Database
- Mongoose: MongoDB object modeling
- bcryptjs: Password hashing
- jsonwebtoken: JWT for authentication
- Passport.js: Authentication middleware
- dotenv: Environment variable management
- CORS: Cross-origin resource sharing
- Node.js (v14 or higher)
- MongoDB instance
- Vercel CLI (optional, for deployment)
-
Clone the repository:
git clone <repository-url> cd user-api
-
Install dependencies:
npm install
-
Create a .env file in the root directory and add the following environment variables:
MONGO_URL=<your-mongodb-connection-string> JWT_SECRET=<your-jwt-secret> PORT=<port-number> # Optional, defaults to 8080
-
Start the server:
npm start
-
The API will be available at http://localhost:8080.
-
POST /api/user/register
- Register a new user.
- Request body:
{ "userName": "exampleUser", "password": "password123", "password2": "password123" } -
POST /api/user/login - Login a user and receive a JWT token. - Request body:
bash { "userName": "exampleUser", "password": "password123" }
-
GET /api/user/favourites
- Retrieve the user's favourites.
- Requires JWT authentication.
-
PUT /api/user/favourites/:id
- Add a favourite item.
- Requires JWT authentication.
-
DELETE /api/user/favourites/:id
- Remove a favourite item.
- Requires JWT authentication.
-
GET /api/user/history Retrieve the user's history. Requires JWT authentication.
-
PUT /api/user/history/:id
- Add a history item.
- Requires JWT authentication.
-
DELETE /api/user/history/:id
- Remove a history item.
- Requires JWT authentication.
Please see artwork-explorer-app for an example of User API usage.
This project can be deployed on Vercel for production. Follow the Next.js deployment documentation for more details.
Author: Nadiia Geras
Date: April 13, 2025