Spaces:
Runtime error
Runtime error
Download src/backend/routes/auth.py from HAK-Tech/ciro-backend: direct link, hf CLI and curl.
- Browser
- Download file 3.45 kB
-
https://huggingface.co/spaces/HAK-Tech/ciro-backend/resolve/main/src/backend/routes/auth.py
- Command line
-
hf download hf://spaces/HAK-Tech/ciro-backend/src/backend/routes/auth.py
-
curl -L -o auth.py https://huggingface.co/spaces/HAK-Tech/ciro-backend/resolve/main/src/backend/routes/auth.py
3.45 kB
| """ | |
| Authentication routes — CNIC verification. | |
| POST /api/auth/verify-cnic | |
| First protected call after phone auth. Links the authenticated | |
| Firebase uid to a user-provided CNIC and creates the Firestore | |
| user record. Requires a valid Firebase JWT. | |
| """ | |
| from __future__ import annotations | |
| from fastapi import APIRouter, Depends, HTTPException, status | |
| from pydantic import BaseModel | |
| from backend.core.auth import get_current_user | |
| from backend.services.cnic import validate_cnic_format | |
| from backend.services.firestore import firestore_service | |
| router = APIRouter(prefix="/api/auth", tags=["Auth"]) | |
| class CnicVerifyRequest(BaseModel): | |
| cnic: str | |
| name: str | |
| class RegisterTokenRequest(BaseModel): | |
| cnic: str | |
| token: str | |
| async def verify_cnic( | |
| payload: CnicVerifyRequest, | |
| user: dict = Depends(get_current_user), | |
| ) -> dict: | |
| """Link an authenticated Firebase uid to a CNIC and create a user record. | |
| - Any well-formatted CNIC (XXXXX-XXXXXXX-X) is accepted — no external | |
| NADRA lookup is performed for the demo. | |
| - If the CNIC is already registered to the same uid, returns the | |
| existing record (idempotent). | |
| - If the CNIC is registered to a *different* uid, returns 409. | |
| """ | |
| if not validate_cnic_format(payload.cnic): | |
| raise HTTPException( | |
| status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, | |
| detail="Invalid CNIC format. Expected XXXXX-XXXXXXX-X (digits only).", | |
| ) | |
| existing = await firestore_service.get_user(payload.cnic) | |
| if existing: | |
| if existing.get("uid") != user["uid"]: | |
| raise HTTPException( | |
| status_code=status.HTTP_409_CONFLICT, | |
| detail="This CNIC is already registered to another account.", | |
| ) | |
| return { | |
| "message": "CNIC already verified", | |
| "cnic": payload.cnic, | |
| "verified": True, | |
| } | |
| await firestore_service.create_user({ | |
| "cnic": payload.cnic, | |
| "uid": user["uid"], | |
| "phone_number": user.get("phone_number"), | |
| "name": payload.name, | |
| "role": "citizen", | |
| "verified": True, | |
| "strike_count": 0, | |
| "blacklisted": False, | |
| "warned": False, | |
| "fcm_token": None, | |
| }) | |
| return { | |
| "message": "CNIC verified successfully", | |
| "cnic": payload.cnic, | |
| "verified": True, | |
| } | |
| async def register_fcm_token( | |
| payload: RegisterTokenRequest, | |
| user: dict = Depends(get_current_user), | |
| ) -> dict: | |
| """Store or update the FCM device token for an authenticated user. | |
| Called on app launch (after CNIC verification) and whenever the token | |
| refreshes. The CNIC in the body is cross-validated against the uid in | |
| the Firebase JWT to prevent token hijacking. | |
| """ | |
| existing = await firestore_service.get_user(payload.cnic) | |
| if not existing: | |
| raise HTTPException( | |
| status_code=status.HTTP_404_NOT_FOUND, | |
| detail="User not found. Complete CNIC verification first.", | |
| ) | |
| if existing.get("uid") != user["uid"]: | |
| raise HTTPException( | |
| status_code=status.HTTP_403_FORBIDDEN, | |
| detail="CNIC does not match the authenticated user.", | |
| ) | |
| await firestore_service.update_user(payload.cnic, {"fcm_token": payload.token}) | |
| return {"message": "FCM token registered", "cnic": payload.cnic} | |