Update code in endpoints/login.post.py
This commit is contained in:
parent
5b169865c6
commit
3038a403fe
@ -1,25 +1,47 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from core.auth import get_current_user_dummy
|
||||
from core.database import fake_users_db
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.post("/login")
|
||||
async def login_demo(
|
||||
username: str = "demo",
|
||||
password: str = "password"
|
||||
@router.post("/vote")
|
||||
async def vote_handler(
|
||||
election_id: str,
|
||||
candidate_id: str,
|
||||
voter_id: str,
|
||||
token: str = "dummy_token"
|
||||
):
|
||||
"""Demo login endpoint"""
|
||||
user = fake_users_db.get(username)
|
||||
if not user or user["password"] != password:
|
||||
raise HTTPException(status_code=400, detail="Invalid credentials")
|
||||
"""Cast a vote in an election"""
|
||||
if voter_id not in fake_users_db:
|
||||
raise HTTPException(status_code=404, detail="Voter not found")
|
||||
|
||||
# Check if user has already voted
|
||||
if "votes" in fake_users_db[voter_id] and election_id in fake_users_db[voter_id]["votes"]:
|
||||
raise HTTPException(status_code=400, detail="User has already voted in this election")
|
||||
|
||||
vote_id = str(uuid.uuid4())
|
||||
|
||||
# Initialize votes if not present
|
||||
if "votes" not in fake_users_db[voter_id]:
|
||||
fake_users_db[voter_id]["votes"] = {}
|
||||
|
||||
# Record the vote
|
||||
fake_users_db[voter_id]["votes"][election_id] = {
|
||||
"vote_id": vote_id,
|
||||
"candidate_id": candidate_id,
|
||||
"timestamp": datetime.utcnow().isoformat()
|
||||
}
|
||||
|
||||
return {
|
||||
"message": "Login successful (demo)",
|
||||
"user": username,
|
||||
"token": "dummy_jwt_token_123",
|
||||
"features": {
|
||||
"rate_limit": 100,
|
||||
"expires_in": 3600
|
||||
"message": "Vote cast successfully",
|
||||
"data": {
|
||||
"vote_id": vote_id,
|
||||
"election_id": election_id,
|
||||
"timestamp": datetime.utcnow().isoformat()
|
||||
},
|
||||
"metadata": {
|
||||
"voter_id": voter_id,
|
||||
"status": "confirmed"
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user