Login and Session Cart

View saved

Goal

This snapshot advances Gearcart Market by teaching you to authenticate users and keep a session shopping cart.

Every numbered folder is a complete app. Run this stage independently, then compare it with the previous folder.

Prerequisites

Use JDK 17+, Maven, and Docker for MySQL stages.

Port 8084 must be free. Copy .env.example before database stages.

  • JDK 17+
  • Maven
  • Docker from part 4 onward

Concepts

Session auth restores the user; the cart lives in session as product id to quantity until checkout.

Thymeleaf renders server HTML; Spring Security owns the session; JPA repositories persist catalog and orders.

Walkthrough

Add login/logout and an Add to Cart action that updates session state.

Read the example, then open the matching snapshot. The repository includes validation, CSRF, and the surrounding structure.

Map<Long,Integer> cart = (Map) session.getAttribute("cart");
cart.merge(productId, 1, Integer::sum);

Run and verify

Enter 07-Login-And-Cart, copy .env.example, start Docker MySQL, and run ./mvnw spring-boot:run on port 8084.

Open http://127.0.0.1:8084. From data lessons onward, Ada and Grace use password123.

docker compose up -d
git clone https://github.com/michaeldunga1/fcc-spring-ecommerce.git
cd fcc-spring-ecommerce/07-Login-And-Cart
cp .env.example .env
./mvnw spring-boot:run

Troubleshooting

A changed session secret invalidates sessions; clear cookies and try again.

For database failures, confirm Docker and the gear_NN name. Never commit .env, target/, or secrets.

  • Read the first exception first
  • Rebuild assets after Tailwind class changes
  • Never commit secrets

Try this

Log in as Ada, add two items, refresh, and confirm the cart badge persists.

Test a happy path and one invalid or unauthorized request.

  • Make one small change
  • Test it in the browser
  • Compare with the next snapshot only after it works

Next: Profiles and Media

Comments

One comment per signed-in account. Comments are saved with this page’s URL.