Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions ai_services/api/endpoints/users.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
from fastapi import APIRouter, HTTPException, status, Depends, Request
from pydantic import BaseModel, EmailStr
from core.auth import get_current_user
from core.database import supabase # Assuming supabase client is initialized in core/database.py
from core.database import (
supabase,
) # Assuming supabase client is initialized in core/database.py
from supabase import Client
from core.auth import get_current_user

router = APIRouter()


# Request schemas
class RegisterRequest(BaseModel):
email: EmailStr
password: str


class LoginRequest(BaseModel):
email: EmailStr
password: str


# Route: Register user
@router.post("/register")
def register_user(payload: RegisterRequest):
try:
result = supabase.auth.sign_up(
{
"email": payload.email,
"password": payload.password
}
{"email": payload.email, "password": payload.password}
)
return {"message": "User registered successfully", "user": result.user}
except Exception as e:
Expand All @@ -36,27 +38,31 @@ def register_user(payload: RegisterRequest):
def login_user(payload: LoginRequest):
try:
result = supabase.auth.sign_in_with_password(
{
"email": payload.email,
"password": payload.password
}
{"email": payload.email, "password": payload.password}
)
if result.session is None:
raise HTTPException(status_code=401, detail="Invalid credentials")

# Insert user data into the users table if needed
user_id = result.user.id if result.user and hasattr(result.user, "id") else None
user_id = result.user.id if result.user and hasattr(
result.user, "id") else None
user_data = {
"id": user_id,
"email": payload.email,
"created_at": result.user.created_at.isoformat() if result.user and hasattr(result.user, "created_at") else None
"created_at": (
result.user.created_at.isoformat()
if result.user and hasattr(result.user, "created_at")
else None
),
}
# print(user_data) Bugs fixing
supabase.table("users").upsert(user_data).execute() # Use upsert to avoid duplicates
supabase.table("users").upsert(
user_data
).execute() # Use upsert to avoid duplicates
return {
"access_token": result.session.access_token,
"refresh_token": result.session.refresh_token,
"user": result.user
"user": result.user,
}
except Exception as e:
raise HTTPException(status_code=401, detail=str(e))
Expand Down