Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions ai_services/api/endpoints/incidents.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

router = APIRouter()


@router.post("/incidents", response_model=IncidentOut)
def report_incident(payload: IncidentCreate, user=Depends(get_current_user)):
try:
Expand Down
28 changes: 16 additions & 12 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,28 +38,30 @@ 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").insert(user_data).execute()

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
4 changes: 3 additions & 1 deletion ai_services/core/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

security = HTTPBearer()


def get_current_user(credentials: HTTPAuthorizationCredentials = Depends(security)):
token = credentials.credentials
try:
# Verify the token with Supabase (optional)
user = supabase.auth.get_user(token)
if user.user is None:
raise HTTPException(status_code=401, detail="Invalid or expired token")
raise HTTPException(
status_code=401, detail="Invalid or expired token")
return user.user
except Exception as e:
raise HTTPException(status_code=401, detail=str(e))
4 changes: 3 additions & 1 deletion ai_services/models/incident.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
from typing import Optional
from datetime import datetime


class IncidentCreate(BaseModel):
title: str
description: str
location: Optional[str] = None
reporter_id: Optional[str] = None # Could be user_id or email


class IncidentOut(BaseModel):
id: str
id: str
title: str
description: str
location: Optional[str]
Expand Down
11 changes: 6 additions & 5 deletions ai_services/services/incident_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@
from uuid import uuid4
from datetime import datetime


def create_incident(data: dict) -> dict:
priority = triage_incident(data["description"])
incident_id = str(uuid4())

new_incident = {
"id": incident_id,
"title": data["title"],
"description": data["description"],
"location": data.get("location"),
"reporter_id": data.get("reporter_id"),
"priority": priority,
"created_at": datetime.utcnow().isoformat()
"created_at": datetime.utcnow().isoformat(),
}

response = supabase.table("incidents").insert(new_incident).execute()
return response.data[0] if response.data else new_incident

Expand All @@ -27,7 +28,7 @@ def create_incident(data: dict) -> dict:
"title": "Unauthorized Access Detected",
"description": "An unknown individual was seen entering the restricted server room.",
"location": "Server Room 3",
"reported_by": "John Doe"
"reported_by": "John Doe",
}
result = create_incident(sample_data)
print(result)
print(result)