In this note
  1. 01Project background
  2. 02Saving each step
  3. 03Progress recovery: from localStorage to a cookie session
  4. 04The 409 that blocked edits
  5. 05Validation on the backend
  6. 06Tests
  7. 07Where the project stands
  8. 08What I took away

The Pilates Health Quiz started as a small project. The goal was to let someone answer a step-by-step health questionnaire, keep the answers on the backend, and produce a basic health assessment at the end. On the surface it is a simple form. Most of the work went into saving each step, restoring progress when someone comes back, validating answers on the server, recognizing the session, and handling the case where a person closes the page halfway through.

Project background

The workflow is easy to describe. A user fills in gender, goals, age, height, current weight, target weight, and exercise frequency, one step at a time. After each step, the frontend syncs the current answer to the backend. If the user closes the page and returns later, the app should restore the previous answers and the step they were on.

The stack:

  • Next.js 16
  • React 19
  • Prisma
  • PostgreSQL / Supabase
  • Zod
  • Vitest

Saving each step

It is tempting to build the questionnaire as one final submission. This project needed every step saved, so someone who leaves midway does not lose progress.

The frontend saves whenever the user selects an answer or submits a numeric input:

PATCH /api/sessions/:sessionId/answers

The request body carries only the increment for the current step:

{
  currentStep: nextStep,
  answers: [
    {
      stepKey: question.key,
      questionKey: question.key,
      value
    }
  ]
}

On the backend, Prisma upsert writes the answer. Changing the same question updates the existing row instead of creating a duplicate.

The first version generated a random sessionId and stored it in frontend localStorage. It worked, with one obvious limitation: session identity depended entirely on data the frontend could read. That is not a server session.

So I moved the session into an HttpOnly cookie.

The flow now:

  1. The first answer triggers POST /api/sessions.
  2. The backend creates the user and the assessment record.
  3. It writes an HttpOnly cookie named pilates_health_quiz_session.
  4. When the user reopens the page, the frontend calls:
GET /api/sessions/current

The backend reads the sessionId from the cookie and returns that user's assessment progress and saved answers.

The frontend no longer reads or writes a local random ID. Session recognition happens on the backend, which is closer to how login and session handling work in a real product.

The 409 that blocked edits

I hit a bug that seems typical of step-by-step flows. When a user went back to an earlier question and changed the answer, the backend returned 409 Conflict and the frontend showed "Unable to save your answer."

The backend validated progress and refused to let currentStep move backward. Editing an earlier question sends a currentStep lower than the furthest step already recorded.

The fix separated the step the UI is on from the furthest step the backend knows:

  • The UI can go back to earlier questions and edit them.
  • When saving, currentStep is never lower than the backend's known maximum.

The backend keeps its anti-skip validation, and users can still fix old answers.

Validation on the backend

The project does not rely on frontend restrictions alone. The backend uses Zod and some custom checks:

  • Height must be between 100 and 250 cm.
  • Weight must be between 30 and 300 kg.
  • Age must be between 13 and 100.
  • Question keys must match known steps.
  • Required prerequisite steps cannot be skipped.

So a request that skips the frontend and posts invalid data straight to the API still fails.

Tests

I wrote tests for the main API flows:

  • Creating a session.
  • Saving answers step by step.
  • Blocking skipped-step submissions.
  • Blocking invalid answers.
  • Restricting access to unpaid results.
  • Unlocking full results after payment.
  • Restoring current progress through the cookie session.

The cookie test checks that Set-Cookie is present, sends that cookie to /api/sessions/current, and confirms that the backend restores the right session and answers.

Where the project stands

The project has the core skeleton of a health assessment product:

  • Step-by-step questionnaire flow
  • Incremental saving at each step
  • Backend persistence
  • Cookie session progress recovery
  • Backend field validation
  • A simulated payment unlock
  • API test coverage

The next step is turning the guest cookie session into a real account system: email login, OAuth, or binding the cookie session to an account. That would let someone recover progress on another device.

What I took away

A questionnaire stops being a form once it has to behave like a product. The decisions that matter are about state: where it is saved, how it is restored, where validation lives, how it is protected, and what happens when things go wrong.

Moving the session from a local random ID to an HttpOnly cookie made the app feel closer to a real product. Session recognition no longer depends on a value the frontend can read. The backend owns it now, which is the beginning of real session handling.