Bookstore API
A RESTful API for managing an online bookstore. The API allows users to perform CRUD operations on books, handle user authentication, and manage password recovery.
- Installation
- Usage
- Features
- Authentication
- API Endpoints
- Error Handling
- Environment Variables
- How to Use
- Contributing
- License
- Clone the repository:
git clone https://github.com/yourusername/yourproject.git
- Navigate to the project directory:
cd yourproject
- Install the dependencies:
npm install
- Set up environment variables '''sh DB_CONNECTION_STRING= JWT_SECRET= '''
- Start the server:
npm start
- The app will be running at
http://localhost:5000
.
User authentication and authorization. CRUD operations for books. Password recovery feature. Secure token-based authentication using JWT. Error handling and validation
- POST /api/auth/register - Register a new user.
- POST /api/auth/login - Login a user.
- POST /password/forgot-password - Request a password reset.
- POST /password/reset-password - Reset the password.
- GET /api/auth/user/ - To get all user (only for admin)
- GET /api/auth/user/:id - To get the user by Id (only the user and admin)
- PUT /api/auth/user/:id - To update the user info by Id
- DELETE /api/auth/user/:id - To delete the user info by Id
- GET /api/books - Get all books.
- POST /api/books - Add a new book (Admin only).
- GET /api/books/:id - Get a single book.
- PUT /api/books/:id - Update a book (Admin only).
- DELETE /api/books/:id - Delete a book (Admin only).
- GET /api/authors - Get all books.
- POST /api/authors - Add a new book (Admin only).
- GET /api/authors/:id - Get a single book.
- PUT /api/authors/:id - Update a book (Admin only).
- DELETE /api/authors/:id - Delete a book (Admin only).
The project uses middleware for error handling:
notFoundError
: Handles 404 errors.errorHandler
: Handles general errors.
The following environment variables need to be set in your .env file: DB_CONNECTION_STRING= JWT_SECRET=
You can interact with the API using tools like Postman or curl. Here are some example requests:
-
Register a new user: curl -X POST http://localhost:3000/api/v1/auth/register -H "Content-Type: application/json" -d '{"username": "testuser", "password": "password123"}'
-
Login a user curl -X POST http://localhost:3000/api/v1/auth/login -H "Content-Type: application/json" -d '{"username": "testuser", "password": "password123"}'
- Fork the repository.
- Create a new branch:
git checkout -b feature/your-feature
- Commit your changes:
git commit -m 'Add some feature'
- Push to the branch:
git push origin feature/your-feature
- Open a pull request.
This project is licensed under the MIT License.