This README provides an overview of the REST API for E-ease and instructions for its easy usage.
-
- Register User
- Login User
- Logout User
- Refresh User Access Token
- Get All Users
- Delete User
- Update User
- Assign User Role
- Delete User Role
- Get Products
- Add Product
- Update Product
- Delete Product
- Add Product Review
- Get Product Review
- Get user cart
- Add To Cart
- Delete Item From Cart
- update Arrays of Item in Cart
- Node.js and npm should be installed on your machine.
-
Clone the repository.
-
Install the required packages using the following command:
npm install
-
Create an
.envfile in your project root folder and add your variables. See.env.samplefor assistance.
- To start the server locally run this command
npm run devTo register a new user, make a POST request to the /register endpoint with the following JSON payload:
{
"username": "example_user",
"email": "user@example.com",
"password": "example_password"
}- if the registration is successful, you will receive a response with a status code of 201 Created and a JSON object containing a message indicating that the user was created, along with the newly created user object:
{
"message": "User created",
"user": {
"username": "example_user",
"email": "user@example.com",
"id": "12345" // User ID
}
}- If there is a conflict (i.e., the username or email is already in use), you will receive a response with a status code of 409 Conflict and a message indicating that the username or email is already in use:
{
"message": "Username or email already in use"
}- If any of the required fields (username, email, or password) are missing from the request, you will receive a response with a status code of 400 Bad Request and a message indicating that these fields are required:
{
"message": "username, email, and password are required"
}You can use tools like Postman, cURL, or any HTTP client library in your preferred programming language to make a POST request to the
/register endpoint.
const registerUser = async () => {
const url = "http://localhost:3500/register";
let responseData;
let errorMessage;
const data = {
username: "example_user",
email: "user@example.com",
password: "example_password",
};
try {
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
if (!response.ok) {
errorMessage = await response.json();
throw new Error(errorMessage.message);
}
responseData = await response.json();
} catch (error:any) {
errorMessage = error.message;
}
return { responseData, errorMessage };
};To authenticate a user, make a POST request to the /auth endpoint with the following JSON payload:
{
"username": "example_user",
"password": "example_password" //User_password
}- If the authentication is successful, you will receive a response with a status code of 200 OK and a JSON object containing the access token and user information:
{
"accessToken": "example_access_token",
"userInfo": {
"username": "example_user",
"email": "user@example.com",
"id": "12345" // User ID
}
}- If either the username or password is missing from the request, you will receive a response with a status code of 400 Bad Request and a message indicating that these fields are required:
{
"message": "Username or password is missing"
}- If the authentication fails (e.g., incorrect username or password), you will receive a response with a status code of 401 Unauthorized and a message indicating the error:
{
"message": "Authentication failed"
}You can use tools like Postman, cURL, or any HTTP client library in your preferred programming language to make a POST request to the
/auth endpoint.
const loginUser = async () => {
const url = "http://localhost:3500/auth";
let responseData;
let errorMessage;
const data = {
username: "example_user",
password: "example_password",
};
try {
const response = await fetch(url, {
method: "POST",
credentials:'include',
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
if (!response.ok) {
errorMessage = await response.json();
throw new Error(errorMessage.message);
}
responseData = await response.json();
} catch (error:any) {
errorMessage = error.message;
}
return { responseData, errorMessage };
};To log out a user, make a GET request to the /logout endpoint.
- If the logout is successful, you will receive a response with a status code of
204 No Content, indicating that the request was successful.
You can use tools like Postman, cURL, or any HTTP client library in your preferred programming language to make a GET request to the
/logout endpoint.
export const logoutUser = async () => {
const url = "http://localhost:3500/logout";
let responseData;
let errorMessage;
try {
const response = await fetch(url, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});
if (!response.ok) {
errorMessage = await response.json();
throw new Error(errorMessage.message);
}
responseData = await response.json();
} catch (error:any) {
errorMessage = error.message;
}
return { responseData, errorMessage };
}- To refresh the access token, make a
GETrequest to the/refreshendpoint. The request should include the refresh token in the cookies.
- If the refresh token is valid and belongs to a user, you will receive a response with a status code of
200 OKand a new access token in the JSON body:
{
"accessToken": "new_access_token"
}- If there is no refresh token in the cookies or the refresh token is invalid, you will receive a response with a status code of
401Unauthorized or403Forbidden depending on the situation.
You can use tools like Postman, cURL, or any HTTP client library in your preferred programming language to make a GET request to the
/refresh endpoint.
export const refreshToken = async () => {
const url = "http://localhost:3500/refresh";
let responseData;
let errorMessage;
try {
const response = await fetch(url, {
method: "GET",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
});
if (!response.ok) {
errorMessage = await response.json();
throw new Error(errorMessage.message);
}
responseData = await response.json();
} catch (error:any) {
errorMessage = error.message;
}
return { responseData, errorMessage };
}