Spaces:
Running
Running
| from fastapi import APIRouter, HTTPException | |
| from pydantic import BaseModel | |
| from supabase import create_client, Client | |
| import os | |
| SUPABASE_URL = "https://ylyxgffttgvvjyrfovpl.supabase.co" | |
| SUPABASE_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InlseXhnZmZ0dGd2dmp5cmZvdnBsIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTA0MTc4NDcsImV4cCI6MjA2NTk5Mzg0N30.a6-biroEh-TNTS8E_uAYYt7mgdY2A-xexjCzYp1MsuI" | |
| supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY) | |
| router = APIRouter() | |
| class UserAuth(BaseModel): | |
| email: str | |
| password: str | |
| def signup(user: UserAuth): | |
| try: | |
| result = supabase.auth.sign_up({ | |
| "email": user.email, | |
| "password": user.password | |
| }) | |
| return {"message": "Signup successful", "user": result.user} | |
| except Exception as e: | |
| raise HTTPException(status_code=400, detail=str(e)) | |
| def login(user: UserAuth): | |
| try: | |
| result = supabase.auth.sign_in_with_password({ | |
| "email": user.email, | |
| "password": user.password | |
| }) | |
| return {"access_token": result.session.access_token, "user_id": result.user.id} | |
| except Exception as e: | |
| raise HTTPException(status_code=401, detail="Invalid credentials") | |