|
import firebase_admin |
|
from firebase_admin import credentials, auth |
|
import os |
|
import json |
|
from fastapi import HTTPException, status |
|
|
|
|
|
cred_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), |
|
"app", "tabble-v1-firebase-adminsdk-fbsvc-8024adcbdf.json") |
|
|
|
|
|
firebase_initialized = False |
|
|
|
try: |
|
|
|
try: |
|
firebase_app = firebase_admin.get_app() |
|
firebase_initialized = True |
|
print("Firebase already initialized") |
|
except ValueError: |
|
|
|
cred = credentials.Certificate(cred_path) |
|
firebase_app = firebase_admin.initialize_app(cred) |
|
firebase_initialized = True |
|
print("Firebase initialized successfully") |
|
except Exception as e: |
|
print(f"Firebase initialization error: {e}") |
|
|
|
|
|
|
|
def verify_phone_number(phone_number): |
|
""" |
|
Verify a phone number and send OTP |
|
Returns a session info token that will be used to verify the OTP |
|
""" |
|
try: |
|
|
|
if not firebase_initialized: |
|
print("Firebase is not initialized, using mock verification") |
|
|
|
|
|
if not phone_number.startswith("+91"): |
|
raise HTTPException( |
|
status_code=status.HTTP_400_BAD_REQUEST, |
|
detail="Phone number must start with +91" |
|
) |
|
|
|
|
|
|
|
|
|
|
|
print(f"Phone verification requested for: {phone_number}") |
|
return {"sessionInfo": "firebase-verification-token", "success": True} |
|
|
|
except HTTPException as e: |
|
|
|
raise e |
|
except Exception as e: |
|
print(f"Error in verify_phone_number: {str(e)}") |
|
raise HTTPException( |
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, |
|
detail=f"Failed to send verification code: {str(e)}" |
|
) |
|
|
|
def verify_otp(phone_number, otp, session_info=None): |
|
""" |
|
Verify the OTP sent to the phone number |
|
Returns a Firebase ID token if verification is successful |
|
|
|
Note: In this implementation, the actual OTP verification is done on the client side |
|
using Firebase Authentication. This function is just for validating the format and |
|
returning a success response. |
|
""" |
|
try: |
|
|
|
if not firebase_initialized: |
|
print("Firebase is not initialized, using mock verification") |
|
|
|
|
|
if not otp.isdigit() or len(otp) != 6: |
|
raise HTTPException( |
|
status_code=status.HTTP_400_BAD_REQUEST, |
|
detail="Invalid OTP format. Must be 6 digits." |
|
) |
|
|
|
|
|
|
|
|
|
print(f"OTP verification successful for: {phone_number}") |
|
return {"idToken": "firebase-id-token", "phone_number": phone_number, "success": True} |
|
|
|
except HTTPException as e: |
|
|
|
raise e |
|
except Exception as e: |
|
print(f"Error in verify_otp: {str(e)}") |
|
raise HTTPException( |
|
status_code=status.HTTP_401_UNAUTHORIZED, |
|
detail=f"Failed to verify OTP: {str(e)}" |
|
) |
|
|