Some checks failed
Build and deploy the backend to staging / Build and push image (pull_request) Successful in 1m48s
Run linting on the backend code / Build (pull_request) Failing after 29s
Run testing on the backend code / Build (pull_request) Failing after 50s
Build and release debug APK / Build APK (pull_request) Failing after 3m50s
Build and deploy the backend to staging / Deploy to staging (pull_request) Successful in 27s
53 lines
1.6 KiB
Python
53 lines
1.6 KiB
Python
"""Endpoints for supabase user handling."""
|
|
import logging
|
|
from fastapi import APIRouter, HTTPException
|
|
|
|
from .supabase import Supabase
|
|
|
|
|
|
# Set up logging and supabase.
|
|
logger = logging.getLogger(__name__)
|
|
supabase = Supabase()
|
|
|
|
# Create fastapi router
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post("/user/create/{email}/{password}")
|
|
def register_user(email: str, password: str) -> str:
|
|
try:
|
|
response = supabase.supabase.auth.admin.create_user({
|
|
"email": email,
|
|
"password": password
|
|
})
|
|
|
|
except Exception as e:
|
|
if e.code == 'email_exists' :
|
|
logger.error(f"Failed to create user : {str(e.code)}")
|
|
raise HTTPException(status_code=422, detail=str(e)) from e
|
|
logger.error(f"Failed to create user : {str(e.code)}")
|
|
raise HTTPException(status_code=500, detail=str(e)) from e
|
|
|
|
# Extract the identity_id and user_id
|
|
user_id = response.user.id
|
|
|
|
logger.info(f"User created successfully, ID: {user_id}")
|
|
return user_id
|
|
|
|
|
|
|
|
@router.post("/user/delete/{user_id}")
|
|
def delete_user(user_id: str):
|
|
|
|
try:
|
|
response = supabase.supabase.auth.admin.delete_user(user_id)
|
|
logger.debug(response)
|
|
except Exception as e:
|
|
if e.code == 'user_not_found' :
|
|
logger.error(f"Failed to delete user : {str(e.code)}")
|
|
raise HTTPException(status_code=404, detail=str(e)) from e
|
|
logger.error(f"Failed to create user : {str(e.code)}")
|
|
raise HTTPException(status_code=500, detail=str(e)) from e
|
|
|
|
logger.info(f"User with ID {user_id} deleted successfully")
|