Fix: Associate comparisons with authenticated user #5

Closed
opened 2026-04-26 08:15:03 +00:00 by TopherMayor · 0 comments
Owner

Summary

Currently POST /api/compare creates comparisons with userId not set (no auth check). The server action createComparison also doesn't set userId. Comparisons need to be linked to the user who created them.

Implementation

  1. In src/app/api/compare/route.ts:

    • Read auth session from request headers
    • Extract userId from session
    • Set userId when inserting into comparisons table
    • Allow anonymous comparisons? (decide: require auth or allow guest with null userId)
  2. In src/app/actions/comparison.ts:

    • createComparison should accept userId and set it
    • getUserComparisons already filters by userId (line 94-100) — just needs to be called with real session
  3. Recommendation: Require authentication for creating comparisons. The compare page should redirect to sign-in if not authenticated.

Files

  • Modify: src/app/api/compare/route.ts (line 55-61 — add userId)
  • Modify: src/app/actions/comparison.ts (line 9-42 — add userId param)
  • Reference: src/lib/auth.ts (Better Auth server config for session extraction)

Acceptance Criteria

  • New comparisons are linked to the authenticated user
  • Unauthenticated users are prompted to sign in before creating
  • Profile page correctly shows only the user's comparisons
## Summary Currently `POST /api/compare` creates comparisons with `userId` not set (no auth check). The server action `createComparison` also doesn't set userId. Comparisons need to be linked to the user who created them. ## Implementation 1. **In `src/app/api/compare/route.ts`**: - Read auth session from request headers - Extract userId from session - Set `userId` when inserting into `comparisons` table - Allow anonymous comparisons? (decide: require auth or allow guest with null userId) 2. **In `src/app/actions/comparison.ts`**: - `createComparison` should accept userId and set it - `getUserComparisons` already filters by userId (line 94-100) — just needs to be called with real session 3. **Recommendation**: Require authentication for creating comparisons. The compare page should redirect to sign-in if not authenticated. ## Files - Modify: `src/app/api/compare/route.ts` (line 55-61 — add userId) - Modify: `src/app/actions/comparison.ts` (line 9-42 — add userId param) - Reference: `src/lib/auth.ts` (Better Auth server config for session extraction) ## Acceptance Criteria - [ ] New comparisons are linked to the authenticated user - [ ] Unauthenticated users are prompted to sign in before creating - [ ] Profile page correctly shows only the user's comparisons
TopherMayor added this to the v0.3 - Auth Integration milestone 2026-04-26 08:15:03 +00:00
TopherMayor added the backendbug labels 2026-04-26 08:15:03 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: TopherMayor/comparaison#5