Skip to content

Commit 68fba2b

Browse files
authored
fix cors on local (#1234)
1 parent ca8863c commit 68fba2b

File tree

5 files changed

+126
-25
lines changed

5 files changed

+126
-25
lines changed

app/README.md

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,24 @@ AgentOps requires several external services. Here's how to set them up:
296296

297297
### Supabase (Required)
298298

299+
**Option A: Local Development (Recommended)**
300+
301+
1. Install Supabase CLI: `brew install supabase/tap/supabase` (or see [docs](https://supabase.com/docs/guides/cli))
302+
2. Initialize and start Supabase locally:
303+
```bash
304+
cd app # Make sure you're in the app directory
305+
supabase init
306+
supabase start
307+
```
308+
3. The local Supabase will provide connection details. Update your `.env` files with:
309+
```
310+
SUPABASE_URL=http://127.0.0.1:54321
311+
SUPABASE_KEY=<anon-key-from-supabase-start-output>
312+
```
313+
4. Run migrations: `supabase db push`
314+
315+
**Option B: Cloud Supabase**
316+
299317
1. Create a new project at [supabase.com](https://supabase.com)
300318
2. Go to Settings → API to get your keys
301319
3. Update your `.env` files with:
@@ -319,7 +337,25 @@ AgentOps requires several external services. Here's how to set them up:
319337
### PostgreSQL (Required)
320338

321339
Configure direct PostgreSQL connection:
322-
1. Use your Supabase PostgreSQL connection details
340+
341+
**For Local Supabase:**
342+
```
343+
POSTGRES_HOST=127.0.0.1
344+
POSTGRES_PORT=54322 # Note: Different port than Supabase API
345+
POSTGRES_USER=postgres
346+
POSTGRES_PASSWORD=postgres
347+
POSTGRES_DATABASE=postgres
348+
349+
# Also add these for SQLAlchemy connections:
350+
SUPABASE_HOST=127.0.0.1
351+
SUPABASE_PORT=54322
352+
SUPABASE_USER=postgres
353+
SUPABASE_PASSWORD=postgres
354+
SUPABASE_DATABASE=postgres
355+
```
356+
357+
**For Cloud Supabase:**
358+
1. Use your Supabase PostgreSQL connection details from Settings → Database
323359
2. Update your `.env` files with:
324360
```
325361
POSTGRES_HOST=your-supabase-host
@@ -391,6 +427,51 @@ bun run lint
391427
bun run format
392428
```
393429

430+
## 🔍 Troubleshooting
431+
432+
### Authentication Issues
433+
434+
**Problem: Login succeeds but immediately redirects back to login page**
435+
436+
This is usually caused by cookie configuration issues between the frontend and backend.
437+
438+
**Solutions:**
439+
440+
1. **Check your environment URLs**: Ensure `NEXT_PUBLIC_API_URL` in dashboard points to your API server:
441+
```bash
442+
# dashboard/.env.local
443+
NEXT_PUBLIC_API_URL=http://localhost:8000 # For local development
444+
```
445+
446+
2. **Verify JWT secret**: Make sure `JWT_SECRET_KEY` is set in your API `.env`:
447+
```bash
448+
# api/.env
449+
JWT_SECRET_KEY=your-secret-key-at-least-32-chars
450+
```
451+
452+
3. **For local development with different ports**: The API automatically adjusts cookie settings for localhost. No manual configuration needed.
453+
454+
4. **For production or custom domains**: Ensure your API and dashboard share the same root domain for cookies to work.
455+
456+
### Database Connection Issues
457+
458+
**Problem: SQLAlchemy connection errors**
459+
460+
Ensure all Supabase database variables are set in `api/.env`:
461+
```bash
462+
SUPABASE_HOST=127.0.0.1 # For local Supabase
463+
SUPABASE_PORT=54322 # Note: Different from API port
464+
SUPABASE_USER=postgres
465+
SUPABASE_PASSWORD=postgres
466+
SUPABASE_DATABASE=postgres
467+
```
468+
469+
### Supabase Seed Data Issues
470+
471+
**Problem: Duplicate key or missing table errors during `supabase start`**
472+
473+
The seed.sql file may have issues. You can temporarily comment out problematic inserts or check if migrations are missing.
474+
394475
## 📦 Production Deployment
395476

396477
### Using Docker Compose

app/api/agentops/app.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"""
1010

1111
from fastapi import FastAPI
12+
from fastapi.middleware.cors import CORSMiddleware
1213
import sentry_sdk
1314

1415
from agentops.api.log_config import logger
@@ -41,6 +42,17 @@
4142
logger.info("⚡️FastAPI app initialized")
4243
logger.info(f"Docs available at: {app.docs_url}" if app.docs_url else "Docs disabled")
4344

45+
# Add CORS middleware for local development
46+
if "localhost" in API_DOMAIN or "127.0.0.1" in API_DOMAIN:
47+
app.add_middleware(
48+
CORSMiddleware,
49+
allow_origins=["http://localhost:3000", "http://127.0.0.1:3000"],
50+
allow_credentials=True,
51+
allow_methods=["*"],
52+
allow_headers=["*"],
53+
)
54+
logger.info("CORS middleware enabled for local development")
55+
4456
# Configure the mounted apps
4557
# TODO this is redundant, but it's just for docs
4658
mounted_apps = {

app/api/agentops/auth/views.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,10 @@ def _create_session_for_response(response: Response, access_token: str) -> Respo
305305
cookie_value = _encode_session_cookie(session)
306306

307307
cookie_domain = _get_api_domain()
308+
# Use secure cookies only with HTTPS
308309
cookie_secure = 'https' in API_URL
310+
# Use 'lax' for HTTP (development) to allow cross-origin requests, 'strict' for HTTPS (production)
311+
cookie_samesite = "lax" if not cookie_secure else "strict"
309312

310313
response.set_cookie(
311314
key=AUTH_COOKIE_NAME,
@@ -314,7 +317,7 @@ def _create_session_for_response(response: Response, access_token: str) -> Respo
314317
secure=cookie_secure, # only send over https in production
315318
domain=cookie_domain, # set cookie for the api domain
316319
max_age=AUTH_COOKIE_EXPIRY,
317-
samesite="strict",
320+
samesite=cookie_samesite,
318321
path="/", # valid across all paths
319322
)
320323

app/api/uv.lock

Lines changed: 7 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/supabase/seed.sql

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,15 @@ INSERT INTO public.user_orgs (
112112
'owner'
113113
);
114114

115-
INSERT INTO public.user_orgs (
116-
user_id, org_id, user_email, role
117-
) VALUES (
118-
'e043e8e0-504d-4e80-83ee-c42c47c63d8b',
119-
'c0000000-0000-0000-0000-000000000000',
120-
121-
'business_user'
122-
);
115+
-- Commented out duplicate key constraint
116+
-- INSERT INTO public.user_orgs (
117+
-- user_id, org_id, user_email, role
118+
-- ) VALUES (
119+
-- 'e043e8e0-504d-4e80-83ee-c42c47c63d8b',
120+
-- 'c0000000-0000-0000-0000-000000000000',
121+
122+
-- 'business_user'
123+
-- );
123124

124125
INSERT INTO public.projects (
125126
id,
@@ -232,17 +233,18 @@ INSERT INTO public.errors (
232233
'2024-03-06 11:15:13.761+00'
233234
);
234235

235-
INSERT INTO public.deployments (
236-
id, project_id, created_at, shutdown_time, image_id, is_active, build_log
237-
) VALUES (
238-
'd1e1f9df-8980-4afc-9041-2e116dc7ad0e',
239-
'0e2bf9df-8980-4afc-9041-2e116dc7ad0e',
240-
'2024-03-05T21:16:00Z',
241-
NULL,
242-
'img-123',
243-
TRUE,
244-
NULL
245-
);
236+
-- Temporarily disabled: deployments table
237+
-- INSERT INTO public.deployments (
238+
-- id, project_id, created_at, shutdown_time, image_id, is_active, build_log
239+
-- ) VALUES (
240+
-- 'd1e1f9df-8980-4afc-9041-2e116dc7ad0e',
241+
-- '0e2bf9df-8980-4afc-9041-2e116dc7ad0e',
242+
-- '2024-03-05T21:16:00Z',
243+
-- NULL,
244+
-- 'img-123',
245+
-- TRUE,
246+
-- NULL
247+
-- );
246248

247249
INSERT INTO public.spans (
248250
id, session_id, agent_id, trace_id, span_id, parent_span_id, name, kind, start_time, end_time, attributes, span_type

0 commit comments

Comments
 (0)