This documentation provides an overview of the Buycars.com API, which allows users to register, login, manage user accounts, and perform inventory-related operations.
To use the Buycars.com API, you need to have Node.js and MongoDB installed on your machine. Follow the steps below to set up the project:
- Clone the repository:
git clone https://github.com/Jishnuraj2001/project_buycars.git
- Navigate to the project directory:
cd backend
- Install dependencies:
npm install
- Create a
.env
file in the project root directory and provide the necessary environment variables. - Start the server:
npm start
The Buycars.com API provides the following endpoints:
-
POST /register: Register a new user. This endpoint requires the following parameters:
name
: Name of the useremail
: Email address of the userpassword
: Password for the user accountrole
: Role of the user (either "customer" or "dealer")
-
POST /login: Login an existing user. This endpoint requires the following parameters:
email
: Email address of the userpassword
: Password for the user account
-
GET /alluser: Get a list of all users. This endpoint requires authentication and authorization.
-
POST /addcar: Add a new car to the inventory. This endpoint requires authentication and authorization. It accepts the following parameters:
brand
: Brand of the carmodel_name
: Model name of the carmodel_year
: Model year of the carcolor
: Color of the carodometer_km
: Odometer reading of the carmajor_accidents
: Number of major accidents the car has been involved inprevious_owners
: Number of previous owners of the carregistration_place
: Place of car registrationdescription
: Description of the carimage
: Image URL of the caruserID
: ID of the user adding the car (authenticated user)
-
GET /allcars: Get a list of all cars in the inventory.
-
DELETE /deletecar/:id: Delete a car from the inventory. This endpoint requires authentication and authorization. It accepts the car ID as a parameter in the URL.
The Buycars.com API uses JWT (JSON Web Tokens) for authentication and authorization. When a user registers or logs in successfully, a JWT token is returned, which must be included in the Authorization
header of subsequent requests.
To authenticate a request, add the JWT token to the Authorization
header in the format: <token>
. The API verifies the token's authenticity and extracts the user ID and role for authorization purposes.
The API also includes middleware functions to ensure that only authorized users can access certain endpoints. The authenticator
middleware validates the JWT token, while the authorizer
middleware checks if the user's role matches the required roles for a specific endpoint.
The Buycars.com API is built using the following technologies:
- Node.js: A JavaScript runtime environment for server-side development.
- Express: A fast and minimalist web application framework for Node.js.
- MongoDB: A NoSQL database for storing user and inventory data.
- Mongoose: An object data modeling (ODM) library for MongoDB and Node.js.
- Bcrypt: A library for hashing passwords.
- JSON Web Tokens (JWT): A standard for securely transmitting information as a JSON object.
The Buycars.com API provides endpoints for user registration, login, managing user accounts, and inventory management. It is built using Node.js, Express, and MongoDB. The API incorporates authentication and authorization using JWT tokens, ensuring that only authenticated and authorized users can access certain resources and perform specific actions.
To get started, follow the instructions in the "Getting Started" section of this documentation. Refer to the provided code for more details on the available endpoints and their parameters.