-
-
Notifications
You must be signed in to change notification settings - Fork 150
Supabase setup #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Supabase setup #134
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
570f5fb
changed env example info
Saahi30 e64d8c2
feat: supabase integration
Saahi30 34e5b0d
chore: configure supabase anon key and fix backend config issues
Saahi30 0872ce1
feat: add supabase connection check on backend startup
Saahi30 ccadd11
chore: add backend README with setup and usage instructions
Saahi30 1e0b112
chore:setup instructions are now consistent and secure
Saahi30 582df75
modified error responses
Saahi30 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| # Supabase Integration Setup | ||
|
|
||
| This project uses Supabase for authentication, database, and backend services. | ||
|
|
||
| ## π Quick Start | ||
|
|
||
| ### Frontend Setup (Next.js) | ||
|
|
||
| 1. **Install dependencies** (already done): | ||
|
|
||
| ```bash | ||
| cd frontend | ||
| npm install | ||
| ``` | ||
|
|
||
| 2. **Configure environment variables**: | ||
| - Copy `.env.example` to `.env.local`: | ||
| ```bash | ||
| cp .env.example .env.local | ||
| ``` | ||
| - Update with your Supabase credentials: | ||
| ```env | ||
| NEXT_PUBLIC_SUPABASE_URL=https://your-project.supabase.co | ||
| NEXT_PUBLIC_SUPABASE_ANON_KEY=your-anon-key-here | ||
| ``` | ||
|
|
||
| 3. **Use the Supabase client**: | ||
|
|
||
| ```typescript | ||
| import { supabase } from "@/lib/supabaseClient"; | ||
|
|
||
| // Example: Fetch data | ||
| const { data, error } = await supabase.from("your_table").select("*"); | ||
| ``` | ||
|
|
||
| ### Backend Setup (FastAPI) | ||
|
|
||
| 1. **Install dependencies**: | ||
|
|
||
| ```bash | ||
| cd backend | ||
| pip install -r requirements.txt | ||
| ``` | ||
|
|
||
| 2. **Configure environment variables**: | ||
| - Copy `env_example` to `.env`: | ||
| ```bash | ||
| cp env_example .env | ||
| ``` | ||
| - Update with your Supabase credentials: | ||
| ```env | ||
| SUPABASE_URL=https://your-project.supabase.co | ||
| SUPABASE_KEY=your-anon-key-here | ||
| ``` | ||
| > **Note:** Use the public Supabase Anon Key (`SUPABASE_KEY`) for backend configuration. Do **not** use or store the Service Role key in your backend `.env` file. | ||
|
|
||
| 3. **Use the Supabase client**: | ||
|
|
||
| ```python | ||
| from app.services.supabase_client import supabase | ||
|
|
||
| # Example: Fetch data | ||
| response = supabase.table('your_table').select('*').execute() | ||
| ``` | ||
|
|
||
| ## π Health Check | ||
|
|
||
| Test your Supabase connection: | ||
|
|
||
| ```bash | ||
| # Start the backend server | ||
| cd backend | ||
| uvicorn app.main:app --reload | ||
|
|
||
| # Check Supabase connection | ||
| curl http://localhost:8000/health/supabase | ||
| ``` | ||
|
|
||
| Expected response: | ||
|
|
||
| ```json | ||
| { | ||
| "connected": true, | ||
| "message": "Supabase client initialized", | ||
| "status": "ready" | ||
| } | ||
| ``` | ||
|
|
||
| ## π Project Structure | ||
|
|
||
| ### Frontend | ||
|
|
||
| ``` | ||
| frontend/ | ||
| βββ lib/ | ||
| β βββ supabaseClient.ts # Supabase client initialization | ||
| βββ types/ | ||
| β βββ supabase.ts # TypeScript types for database | ||
| βββ .env.example # Environment variables template | ||
| ``` | ||
|
|
||
| ### Backend | ||
|
|
||
| ``` | ||
| backend/ | ||
| βββ app/ | ||
| β βββ core/ | ||
| β β βββ config.py # Configuration settings | ||
| β βββ services/ | ||
| β β βββ supabase_client.py # Supabase client initialization | ||
| β βββ api/ | ||
| β βββ routes/ | ||
| β βββ health.py # Health check endpoints | ||
| βββ env_example # Environment variables template | ||
| ``` | ||
|
|
||
| ## π Environment Variables | ||
|
|
||
| ### Frontend (Next.js) | ||
|
|
||
| - `NEXT_PUBLIC_SUPABASE_URL`: Your Supabase project URL | ||
| - `NEXT_PUBLIC_SUPABASE_ANON_KEY`: Supabase anon/public key (safe for client-side) | ||
|
|
||
| ### Backend (FastAPI) | ||
|
|
||
| - `SUPABASE_URL`: Your Supabase project URL | ||
| - `SUPABASE_KEY`: Supabase anon/public key (safe for backend, matches config.py) | ||
| - `DATABASE_URL`: (Optional) Direct PostgreSQL connection string | ||
| - `ALLOWED_ORIGINS`: CORS allowed origins (comma-separated) | ||
|
|
||
| ## π Security Notes | ||
|
|
||
| 1. **Never commit `.env` files** - they are in `.gitignore` | ||
| 2. **Frontend uses anon key** - safe for browser, limited permissions | ||
| 3. **Backend uses anon key** - safe for backend, never expose service role key or store it in backend environment files | ||
| 4. **Rotate keys if exposed** - generate new keys in Supabase dashboard | ||
| 5. **Use environment-specific keys** - different keys for dev/staging/prod | ||
|
|
||
| ## π Next Steps | ||
|
|
||
| 1. Create your database tables in Supabase Dashboard | ||
| 2. Update `frontend/types/supabase.ts` with your table types | ||
| 3. Implement authentication flows | ||
| 4. Add Row Level Security (RLS) policies in Supabase | ||
|
|
||
| ## π οΈ Useful Commands | ||
|
|
||
| ```bash | ||
| # Frontend development | ||
| cd frontend && npm run dev | ||
|
|
||
| # Backend development | ||
| cd backend && uvicorn app.main:app --reload | ||
|
|
||
| # Install new dependencies | ||
| cd frontend && npm install <package> | ||
| cd backend && pip install <package> && pip freeze > requirements.txt | ||
| ``` | ||
|
|
||
| ## π Documentation | ||
|
|
||
| - [Supabase Documentation](https://supabase.com/docs) | ||
| - [Next.js + Supabase Guide](https://supabase.com/docs/guides/getting-started/quickstarts/nextjs) | ||
| - [Python + Supabase Guide](https://supabase.com/docs/reference/python/introduction) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| """ | ||
| Health check routes for monitoring service status | ||
| """ | ||
| from fastapi import APIRouter, HTTPException | ||
|
|
||
| router = APIRouter(prefix="/health", tags=["health"]) | ||
|
|
||
| @router.get("/") | ||
| def health_check(): | ||
| """Basic health check endpoint""" | ||
| return {"status": "healthy", "message": "Backend is running"} | ||
|
|
||
| @router.get("/supabase") | ||
| def check_supabase(): | ||
| """ | ||
| Check Supabase connection status. | ||
| This endpoint attempts to query Supabase to verify the connection. | ||
| """ | ||
| try: | ||
| from app.services.supabase_client import supabase | ||
|
|
||
| # Attempt a simple query to verify connection | ||
| response = supabase.table("_supabase_test").select("*").limit(1).execute() | ||
|
|
||
| return { | ||
| "connected": True, | ||
| "message": "Supabase connection is working!", | ||
| "status": "healthy" | ||
| } | ||
| except Exception as e: | ||
| error_msg = str(e) | ||
| # Detect table-not-found error (Supabase/PostgREST or DB error) | ||
| if ( | ||
| "does not exist" in error_msg or | ||
| "relation" in error_msg and "does not exist" in error_msg or | ||
| "Could not find the table" in error_msg or | ||
| "PGRST205" in error_msg | ||
| ): | ||
| return { | ||
| "connected": True, | ||
| "message": "Supabase client initialized (no tables queried yet)", | ||
| "status": "ready", | ||
| "note": error_msg | ||
| } | ||
| # For any other error, treat as unhealthy | ||
| return { | ||
| "connected": False, | ||
| "message": "Supabase connection failed", | ||
| "status": "unhealthy", | ||
| "note": error_msg | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| """ | ||
| Services module for backend application | ||
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from supabase import create_client, Client | ||
| from app.core.config import settings | ||
|
|
||
| # Initialize Supabase client with anon key (public) | ||
| supabase: Client = create_client(settings.supabase_url, settings.supabase_key) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.