CIA/e-voting-system/.claude/VOTE_CHECK_EXPLANATION.md
E-Voting Developer 3efdabdbbd fix: Implement vote check endpoint in frontend API proxy
- Created `/frontend/app/api/votes/check/route.ts` to handle GET requests for checking if a user has voted in a specific election.
- Added error handling for unauthorized access and missing election ID.
- Forwarded requests to the backend API and returned appropriate responses.
- Updated `/frontend/app/api/votes/history/route.ts` to fetch user's voting history with error handling.
- Ensured both endpoints utilize the authorization token for secure access.
2025-11-10 02:56:47 +01:00

4.4 KiB

🎯 Vote Check - Before & After

The Problem You Reported

GET http://localhost:3000/api/votes/check?election_id=1

Response:
<!DOCTYPE html>
<title>404: This page could not be found.</title>
...

Why? The frontend didn't have a route to handle this endpoint.


The Solution

Created: /frontend/app/api/votes/check/route.ts

This simple proxy route forwards requests to the backend.


Before ( Broken)

User visits voting page
  ↓
Page load
  ↓
Try: GET /api/votes/check?election_id=1
  ↓
Next.js: "I don't have this route!"
  ↓
Response: 404 HTML page ❌

After ( Fixed)

User visits voting page
  ↓
Page load
  ↓
Try: GET /api/votes/check?election_id=1
  ↓
Frontend proxy: "Let me forward this to backend..."
  ↓
Backend: "Query database... User has not voted"
  ↓
Response: { "has_voted": false } ✅
  ↓
Frontend: "Show voting form" ✅

What Happens on Page Load

Scenario 1: User Hasn't Voted Yet

1. Visit: /dashboard/votes/active/1
2. Page loads
3. Check vote status: /api/votes/check?election_id=1
4. Response: { "has_voted": false }
5. Show: Voting form ✅

Scenario 2: User Already Voted

1. Visit: /dashboard/votes/active/1
2. Page loads  
3. Check vote status: /api/votes/check?election_id=1
4. Response: { "has_voted": true }
5. Show: "Vote Done" page ✅
   (NO voting form shown)

The Flow

┌─────────────────────────────────────────────────────────┐
│  Browser: /dashboard/votes/active/1                    │
└─────────────────┬───────────────────────────────────────┘
                  │
                  │ useEffect on mount
                  ↓
        ┌─────────────────────────────────────┐
        │  Fetch election details             │
        │  + Check vote status                │
        └────────┬────────────────────────────┘
                 │
    ┌────────────┴────────────┐
    ↓                         ↓
Voted?                    Not voted?
    │                         │
    │ YES                     │ NO
    ↓                         ↓
┌─────────────┐       ┌──────────────┐
│ Vote Done   │       │ Voting Form  │
│ Page        │       │              │
└─────────────┘       └──────────────┘

Code Change

New File: /frontend/app/api/votes/check/route.ts

export async function GET(request: NextRequest) {
  const token = request.headers.get('authorization')?.split(' ')[1]
  const electionId = request.nextUrl.searchParams.get('election_id')

  // Forward to backend
  const response = await fetch(
    `${process.env.BACKEND_URL || 'http://localhost:8000'}/api/votes/check?election_id=${electionId}`,
    {
      headers: {
        Authorization: `Bearer ${token}`,
      },
    }
  )

  const data = await response.json()
  return NextResponse.json(data)
}

What Already Existed

Backend has: /api/votes/check (lines 766-791 of votes.py) Frontend calls it on page load (lines 60-77 of [id]/page.tsx) Frontend API proxy was missing → NOW CREATED!


Summary

Item Status
Backend endpoint Already existed
Frontend page load call Already existed
Frontend API proxy route Was missing → Now created
Vote check on page load Works correctly
Vote check on submit only Not needed

Testing

# 1. Restart frontend (to load new route)
docker compose restart frontend && sleep 3

# 2. Visit page
# http://localhost:3000/dashboard/votes/active/1

# 3. Check Network tab (F12)
# Should see: GET /api/votes/check?election_id=1 → 200 OK

# 4. Result
# - If you voted: "Vote Done" page shows
# - If you didn't vote: Voting form shows

Status

FIXED - Ready to test!

The endpoint now works correctly and checks vote status on page load, not on submit.