Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
e0c21b3
start of Swagger YAML file
XTruckDriver Jan 14, 2025
09fcb64
update launch config for tests and update packages
XTruckDriver Mar 10, 2025
49e605b
GET-brands: add test
XTruckDriver Mar 10, 2025
f82fa01
GET-brands: implement route
XTruckDriver Mar 10, 2025
11f2f57
GET-products: add tests
XTruckDriver Mar 11, 2025
7994864
GET-products: implement route
XTruckDriver Mar 11, 2025
a07bf45
GET-brands-id-products: add tests
XTruckDriver Mar 11, 2025
6bbe15f
GET-brands-id-products: add test
XTruckDriver Mar 13, 2025
ce180b5
GET-brands-id-products: implement route
XTruckDriver Mar 13, 2025
8dc4f6f
POST-login: add tests
XTruckDriver Mar 21, 2025
8de4af1
POST-login: implement route
XTruckDriver Mar 24, 2025
12a34fa
POST-login: add test for invalid login info
XTruckDriver Mar 25, 2025
af8f372
implement verifyToken middleware
XTruckDriver Mar 29, 2025
ecb720b
add temporary test and route to test middleware
XTruckDriver Mar 31, 2025
3e601b0
remove temporary tests and route
XTruckDriver Mar 31, 2025
425c202
GET /me/cart: add test
XTruckDriver Mar 31, 2025
98036a8
GET /me/cart: implement route
XTruckDriver Mar 31, 2025
2d827b2
POST /me/cart: add tests
XTruckDriver Apr 1, 2025
d22a543
POST /me/cart: implement route
XTruckDriver Apr 2, 2025
28c0f2b
DELETE /me/cart/:productId -Add test
XTruckDriver Apr 3, 2025
f159093
DELETE /me/cart/:productId -Implement route
XTruckDriver Apr 3, 2025
5ab96d6
PUT /me/cart/:productId - add tests
XTruckDriver Apr 5, 2025
5d5be51
PUT /me/cart/:productId - implement route
XTruckDriver Apr 5, 2025
c7c485b
Merge branch 'GET-products'
XTruckDriver Apr 11, 2025
be73311
Merge branch 'GET-brands-id-products'
XTruckDriver Apr 11, 2025
553f516
merge 'GET-brands-id-products' into master
XTruckDriver Apr 11, 2025
b31ca7f
merge all-cart-routes
XTruckDriver Apr 11, 2025
a86a5cb
save edits to swagger file
XTruckDriver Jun 3, 2025
2de2896
Add test to validate brand ID in GET /brands/:id/products
XTruckDriver Jun 4, 2025
4b217b1
implement code to make test pass for brand ID validation in GET /bran…
XTruckDriver Jun 4, 2025
87b8b20
update Swagger file with validation for GET /brands/:id/products
XTruckDriver Jun 5, 2025
c9fb93d
update verifyToken middleware: if token is valid, extend exp by an hour
XTruckDriver Jun 9, 2025
d42c480
update protected routes to return the new, extended token with succes…
XTruckDriver Jun 9, 2025
5bb4ecf
update swagger file
XTruckDriver Jun 11, 2025
22099f0
more updates to swagger file: add post /me/cart route
XTruckDriver Jun 14, 2025
445f529
more swagger file updates
XTruckDriver Jun 23, 2025
5f9ba3b
add CORS headers, allow localhost:3000
XTruckDriver Jun 23, 2025
1780780
Add validation for brand ID to GET /brands/:id/products
XTruckDriver Jul 12, 2025
a06ed25
Update response from POST /login to return users cart with token
XTruckDriver Jul 12, 2025
640d3b6
Add validations and fix bug of how items appear in cart
XTruckDriver Jul 20, 2025
94876a1
add validation for productId to DELETE /me/cart/:productId
XTruckDriver Jul 21, 2025
dc99230
add validation for productId, quantity, and that the item exists in c…
XTruckDriver Jul 21, 2025
55dddd1
more Swagger updates
XTruckDriver Jul 22, 2025
8126f01
Update README file, and dependencies
XTruckDriver Jul 31, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
{
// Use IntelliSense to learn about possible Node.js debug attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Sunglasses.io",
"program": "${workspaceRoot}/app/server.js"
}
]
}
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Run Mocha Tests",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"-u",
"bdd",
"--timeout",
"999999",
"--colors",
"${workspaceFolder}/test/server.test.js"
],
"cwd": "${workspaceFolder}",
"console": "integratedTerminal"
},
{
"type": "node",
"request": "launch",
"name": "Run Server",
"program": "${workspaceFolder}/app/server.js",
"cwd": "${workspaceFolder}"
}
]
}
178 changes: 175 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,177 @@
## Sunglasses.io Server
# Sunglasses.io Server

This project has been created by a student at Project Shift, a software engineering fellowship located in Downtown Durham. The work in this repository is wholly of the student based on a sample starter project that can be accessed by looking at the repository that this project forks.
> This project has been created by a student at Parsity, an online software engineering fellowship. The work in this repository is wholly of the student based on a sample starter project that can be accessed by looking at the repository that this project forks.
>
> If you have any questions about this project or the program in general, visit [Parsity.io](https://www.parsity.io/)

If you have any questions about this project or the program in general, visit projectshift.io or email [email protected].
---

<br>
Welcome to the Sunglasses.io Server! This is a Node.js/Express-based backend project that provides a RESTful API for managing sunglasses brands, products, and user carts. The API uses JWT (JSON Web Token) authentication for protected routes, with a token renewal mechanism to extend session validity.

## Project Overview

This API allows users to:

- Retrieve a list of brands and their products.
- Authenticate and manage a personal shopping cart with CRUD operations (create, read, update, delete).
- Test the API interactively via Swagger UI.

## Requirements

- **Node.js**: Version 14.x or higher (install from [nodejs.org](https://nodejs.org/)).
- **npm**: Comes with Node.js (verify with 'npm -v').
- **Git**: For cloning the repository (install from [git-scm.com](http://git-scm.com/)).
- A code editor (e.g., Visual Studio Code) is recommended.

## Getting Started

<br>

<u>**Install dependencies**</u>:

```
npm install
```

- This installs required packages: `express`, `body-parser`, `jsonwebtoken`, `swagger-ui-express`, `yamljs`, and `cors`:

- Ensure the node_modules folder is created and no errors occur during installation.
<br>
<br>

<u>**Start the Server**</u>

```
npm start
```

- The API runs on http://localhost:3000.

- Press `Ctrl+C` to stop the server.
<br>
<br>

## Testing the API using Swagger UI

<br>

- with server running, open browser and go to http://localhost:3000/api-docs to view interactive API documentation.

<br>

**Non-Protected Routes** (can be accessed without authentication):

- `GET /products`
- `GET /brands`
- `GET /brands/:id/products`

<br>

**User Login Route** (access to cart and JWT):

- `POST /login`

- click "Try it out",
- sample account login is provided:

```
{"username": "yellowleopard753", "password": "jonjon"}
```

- click "Execute".

- copy the token from the response (e.g., eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...) to paste into the next request.

<br>

**Protected Routes** (require valid JWT token to access):

- `GET /me/cart`
- `POST /me/cart`
- `DELETE /me/cart/:productId`
- `PUT /me/cart/:productId`

<br>

<u>**To access protected routes:**</u>

- all protected routes return a new token with an extended expiration (1 hour from request time)
- click "Try it out"
- in the "Authorization" box, paste the `token` with `Bearer` prefix
- token comes from either the initial login or a prior response
- ensure there is a space between `Bearer`(capital B) and the `token` and do not use quotes.
- example:
```
Bearer eyJhbGciOiJIUzI...9MiVqLdMrKohxSL4
```
- if necessary, include any required parameters
- click "Execute"
- response will include an updated `cart` and a new `token`
- Repeat process for subsequent requests to maintain a valid session

<br>

---

### Using Postman

- server should be already running, if not use command:
```
npm start
```
- <u>No</u> authentication needed for **non-protected routes**:

- `GET localhost:3000/products`
- `GET localhost:3000/brands`
- `GET localhost:3000/brands/:id/products`

<br>

**<u>Obtain a Token</u>**:

- Send a `POST` request to `localhost:3000/login`

- include the following in the body:
```
{"username": "yellowleopard753", "password": "jonjon"}
```

- Copy the `token` from the response to authenticate future requests.

<br>

**<u>Testing Protected Routes</u>** (require valid JWT token to access):

- Valid token must be provided to access these routes:

- `GET localhost:3000/me/cart`
- `POST localhost:3000/me/cart`
- `DELETE localhost:3000/me/cart/:productId`
- `PUT localhost:3000/me/cart/:productId`

<br>

- **Example** - create new `POST /me/cart` request which adds item to user's cart:

- select POST
- enter `localhost:3000/me/cart` into the text box
- in "Authorization" tab, under "Auth Type" drowdown, select "Bearer Token"
- paste the `token` into the "Token" textbox
- if necessary, add any required parameters
- for `POST /me/cart` request, paste the following into req.body:
```
{"productId": "1", "quantity": 10}
```
- the response will include the updated `cart` and a new `token`.

<br>

## Token Management Notes

- The API uses a sliding expiration model: the token’s expiration is extended by 1 hour with each protected route request, and a new token is returned in the response.
- The initial token remains valid until its original 1-hour expiration, which is why you may not need to update it immediately in Postman or Swagger UI during short testing sessions.
<br>
<br>

**Important**: When using a frontend, the client must manage and use the new token from each response to ensure continuous validity. Failure to update the token after its expiration will result in a 401 Unauthorized error.
Loading