Task 1's Campus Marketplace API with JWT login/register on the server and a React client with a login flow.
npm run install:all # installs server/ and client/ (npm workspaces)
npm run dev # API on :4000, client on :5175
Create server/.env yourself with:
PORT=4000
MONGO_URI=mongodb://tasks:pass1234@ac-j3acrgb-shard-00-00.lueesfz.mongodb.net:27017,ac-j3acrgb-shard-00-01.lueesfz.mongodb.net:27017,ac-j3acrgb-shard-00-02.lueesfz.mongodb.net:27017/?ssl=true&replicaSet=atlas-6to6iy-shard-0&authSource=admin&appName=Cluster0
JWT_SECRET=change-me
JWT_EXPIRES_IN=7d
JWT_SECRET is the key the server signs and verifies login tokens with —
anyone who knows it can forge a token for any user, so replace change-me
with a long random value. Generate one with:
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
Paste the output after JWT_SECRET= and restart the server. Changing the
secret logs everyone out, because tokens signed with the old one stop being
valid.
POST /api/auth/register,POST /api/auth/login→{ token, user };GET /api/auth/me(requires token).middleware/auth.js—requireAuthreadsAuthorization: Bearer <token>, verifies it withJWT_SECRETand setsreq.user = { id, name }.User.comparePassword(); thepasswordfield stores the bcrypt hash.- Listings: reading (
GET /,GET /:id) stays public, and removed listings stay hidden unless?includeRemoved=true. Creating, editing and removing require a token.selleris set fromreq.user.id(sending it in the body is rejected), and only the seller can edit or remove a listing (403otherwise). Removing is still a soft delete (status: 'removed'). Listings are populated with the seller'sname/email. - Users:
POST /api/userswas removed (use/api/auth/register);PATCH/DELETE /api/users/:idrequire a token and only work on your own account.
AuthContextstores the token inlocalStorage, restores the session via/auth/me, andapi.jsattaches the token to every request.- Pages: Login, Register, Listings (list, edit/remove on your own listings). ListingForm is left for you — see below.
Everything above is already working. Your job is the frontend of creating a
listing: client/src/pages/ListingForm.jsx. It is already routed at
/listings/new and /listings/:id, both behind ProtectedRoute, and the
"Sell an Item" nav link and the "Edit" buttons already point to it.
This is roughly what the finished page should look like when creating a listing (filled in with example values — the status select only appears when editing):
Render inputs bound to the form state:
title— text input (e.g.Calculus textbook)price— number input,0or more (store it as a number, not a string)category— select:textbooks,electronics,furniture,clothing,othercondition— select:new,like-new,used,worndescription— textarea (optional)status— select:active,sold— only shown when editing
The select values must match these strings exactly — anything else is
rejected by the server. Implement onChange so every input updates form.
In onSubmit, send POST /api/listings with the form fields using the api
instance from client/src/api.js (it already attaches your token). On
success, navigate back to /listings. On failure, show the server's
message in the error box — try it: a negative price returns 400.
Do not send seller — the server takes the seller from your token and
rejects the request if you send it.
When the URL has an id, load the listing with GET /api/listings/:id and
fill the form with its title, price, category, condition, status
and description. On submit, send PATCH /api/listings/:id instead of
POST — this is how a seller marks an item as sold. Editing someone
else's listing returns 403 — show that message too.
You're expected to use AI tools while building this. But you should be able to explain, for any line in your component, why it's there and what happens if you delete it. We will ask.
