22CodeIntel Backend API
33FastAPI backend for codebase intelligence
44"""
5- from fastapi import FastAPI , HTTPException , Header , WebSocket , WebSocketDisconnect
5+ from fastapi import FastAPI , HTTPException , Header , WebSocket , WebSocketDisconnect , Depends
66from fastapi .middleware .cors import CORSMiddleware
77from pydantic import BaseModel
88from typing import Optional , List
2525from services .supabase_service import get_supabase_service
2626from services .input_validator import InputValidator , CostController
2727
28+ # Import routers
29+ from routes .auth import router as auth_router
30+ from middleware .auth import get_current_user
31+
2832app = FastAPI (
2933 title = "CodeIntel API" ,
3034 description = "Codebase Intelligence API for MCP" ,
3135 version = "0.2.0"
3236)
3337
38+ # Include routers
39+ app .include_router (auth_router )
40+
3441# CORS middleware
3542app .add_middleware (
3643 CORSMiddleware ,
@@ -135,21 +142,23 @@ async def health_check():
135142
136143
137144@app .get ("/api/repos" )
138- async def list_repositories (api_key : str = Header ( None , alias = "Authorization" )):
139- """List all repositories"""
140- verify_api_key ( api_key )
145+ async def list_repositories (current_user : dict = Depends ( get_current_user )):
146+ """List all repositories for authenticated user """
147+ user_id = current_user [ "user_id" ]
141148
149+ # TODO: Filter repos by user_id once we add user_id column to repositories table
150+ # For now, return all repos (will fix in next section)
142151 repos = repo_manager .list_repos ()
143152 return {"repositories" : repos }
144153
145154
146155@app .post ("/api/repos" )
147156async def add_repository (
148157 request : AddRepoRequest ,
149- api_key : str = Header ( None , alias = "Authorization" )
158+ current_user : dict = Depends ( get_current_user )
150159):
151160 """Add a new repository with validation and cost controls"""
152- key_data = verify_api_key ( api_key )
161+ user_id = current_user [ "user_id" ]
153162
154163 # Validate repository name
155164 valid_name , name_error = InputValidator .validate_repo_name (request .name )
@@ -162,10 +171,9 @@ async def add_repository(
162171 raise HTTPException (status_code = 400 , detail = f"Invalid Git URL: { url_error } " )
163172
164173 # Check repository limit
165- user_id = key_data .get ("user_id" )
166- api_key_hash = hashlib .sha256 (api_key .replace ("Bearer " , "" ).encode ()).hexdigest ()
174+ user_id_hash = hashlib .sha256 (user_id .encode ()).hexdigest ()
167175
168- can_add , limit_error = cost_controller .check_repo_limit (user_id , api_key_hash )
176+ can_add , limit_error = cost_controller .check_repo_limit (user_id , user_id_hash )
169177 if not can_add :
170178 raise HTTPException (status_code = 429 , detail = limit_error )
171179
@@ -175,7 +183,7 @@ async def add_repository(
175183 git_url = request .git_url ,
176184 branch = request .branch ,
177185 user_id = user_id ,
178- api_key_hash = api_key_hash
186+ api_key_hash = user_id_hash
179187 )
180188
181189 # Check repo size before allowing indexing
0 commit comments