Spaces:
Running
Running
refactor: remove unnecessary sign up page
Browse files- auth/routes.py +0 -120
- main.py +8 -0
auth/routes.py
CHANGED
|
@@ -10,126 +10,6 @@ from typing import Optional
|
|
| 10 |
|
| 11 |
router = APIRouter(prefix="/api/auth", tags=["Auth"])
|
| 12 |
|
| 13 |
-
|
| 14 |
-
class LoginRequest(BaseModel):
|
| 15 |
-
email: str
|
| 16 |
-
password: str
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
class SignupRequest(BaseModel):
|
| 20 |
-
name: str
|
| 21 |
-
email: str
|
| 22 |
-
password: str
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
@router.post("/login")
|
| 26 |
-
async def login(body: LoginRequest):
|
| 27 |
-
supabase = get_auth_supabase()
|
| 28 |
-
|
| 29 |
-
# Real Supabase auth
|
| 30 |
-
if supabase:
|
| 31 |
-
try:
|
| 32 |
-
res = supabase.auth.sign_in_with_password({"email": body.email, "password": body.password})
|
| 33 |
-
sb_user = res.user
|
| 34 |
-
session = res.session
|
| 35 |
-
if not sb_user or not session:
|
| 36 |
-
raise HTTPException(401, "Invalid email or password")
|
| 37 |
-
# Fetch profile from our table to get custom name/avatar
|
| 38 |
-
user_profile = get_user_by_id(str(sb_user.id))
|
| 39 |
-
if not user_profile:
|
| 40 |
-
try:
|
| 41 |
-
user_profile = create_user(
|
| 42 |
-
name=sb_user.user_metadata.get("name", body.email.split("@")[0]),
|
| 43 |
-
email=sb_user.email,
|
| 44 |
-
password="",
|
| 45 |
-
user_id=str(sb_user.id)
|
| 46 |
-
)
|
| 47 |
-
except Exception:
|
| 48 |
-
pass
|
| 49 |
-
|
| 50 |
-
if not user_profile:
|
| 51 |
-
# Final fallback to metadata
|
| 52 |
-
user_profile = {
|
| 53 |
-
"id": str(sb_user.id),
|
| 54 |
-
"name": sb_user.user_metadata.get("name", body.email.split("@")[0]),
|
| 55 |
-
"email": sb_user.email,
|
| 56 |
-
"avatar": sb_user.user_metadata.get("avatar_url", ""),
|
| 57 |
-
"usage": {"used": 0, "limit": 10, "remaining": 10}
|
| 58 |
-
}
|
| 59 |
-
|
| 60 |
-
return {
|
| 61 |
-
"token": session.access_token,
|
| 62 |
-
"user": user_profile,
|
| 63 |
-
}
|
| 64 |
-
except HTTPException:
|
| 65 |
-
raise
|
| 66 |
-
except Exception as e:
|
| 67 |
-
raise HTTPException(401, f"Invalid email or password: {e}")
|
| 68 |
-
|
| 69 |
-
# Dev fallback (no Supabase)
|
| 70 |
-
user = get_user_by_email(body.email)
|
| 71 |
-
if not user or user.get("password") != body.password:
|
| 72 |
-
raise HTTPException(401, "Invalid email or password")
|
| 73 |
-
return {
|
| 74 |
-
"token": str(uuid.uuid4()),
|
| 75 |
-
"user": user,
|
| 76 |
-
}
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
@router.post("/signup")
|
| 80 |
-
async def signup(body: SignupRequest):
|
| 81 |
-
supabase = get_auth_supabase()
|
| 82 |
-
|
| 83 |
-
# Real Supabase auth
|
| 84 |
-
if supabase:
|
| 85 |
-
try:
|
| 86 |
-
res = supabase.auth.sign_up({
|
| 87 |
-
"email": body.email,
|
| 88 |
-
"password": body.password,
|
| 89 |
-
"options": {"data": {"name": body.name}},
|
| 90 |
-
})
|
| 91 |
-
sb_user = res.user
|
| 92 |
-
session = res.session
|
| 93 |
-
if not sb_user:
|
| 94 |
-
raise HTTPException(400, "Signup failed")
|
| 95 |
-
# session can be None if email confirmation is required
|
| 96 |
-
# Create profile in our database
|
| 97 |
-
try:
|
| 98 |
-
create_user(
|
| 99 |
-
name=body.name,
|
| 100 |
-
email=sb_user.email,
|
| 101 |
-
password="",
|
| 102 |
-
user_id=str(sb_user.id)
|
| 103 |
-
)
|
| 104 |
-
except Exception:
|
| 105 |
-
pass
|
| 106 |
-
|
| 107 |
-
return {
|
| 108 |
-
"token": token,
|
| 109 |
-
"user": {
|
| 110 |
-
"id": str(sb_user.id),
|
| 111 |
-
"name": body.name,
|
| 112 |
-
"email": sb_user.email,
|
| 113 |
-
"avatar": "",
|
| 114 |
-
"usage": {"used": 0, "limit": 10, "remaining": 10}
|
| 115 |
-
},
|
| 116 |
-
}
|
| 117 |
-
except HTTPException:
|
| 118 |
-
raise
|
| 119 |
-
except Exception as e:
|
| 120 |
-
raise HTTPException(400, f"Signup failed: {e}")
|
| 121 |
-
|
| 122 |
-
# Dev fallback (no Supabase)
|
| 123 |
-
try:
|
| 124 |
-
user = create_user(body.name, body.email, body.password)
|
| 125 |
-
except ValueError as e:
|
| 126 |
-
raise HTTPException(400, str(e))
|
| 127 |
-
return {
|
| 128 |
-
"token": str(uuid.uuid4()),
|
| 129 |
-
"user": user,
|
| 130 |
-
}
|
| 131 |
-
|
| 132 |
-
|
| 133 |
@router.delete("/me")
|
| 134 |
async def delete_account(user_id: str = Depends(get_current_user_id)):
|
| 135 |
delete_user_data(user_id)
|
|
|
|
| 10 |
|
| 11 |
router = APIRouter(prefix="/api/auth", tags=["Auth"])
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
@router.delete("/me")
|
| 14 |
async def delete_account(user_id: str = Depends(get_current_user_id)):
|
| 15 |
delete_user_data(user_id)
|
main.py
CHANGED
|
@@ -64,6 +64,14 @@ app = FastAPI(
|
|
| 64 |
lifespan=lifespan,
|
| 65 |
)
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
app.add_middleware(
|
| 68 |
CORSMiddleware,
|
| 69 |
allow_origins=settings.cors_allowed_origins or ["*"],
|
|
|
|
| 64 |
lifespan=lifespan,
|
| 65 |
)
|
| 66 |
|
| 67 |
+
@app.middleware("http")
|
| 68 |
+
async def normalize_path(request, call_next):
|
| 69 |
+
# Fix double slashes in paths (e.g., //api/usage -> /api/usage)
|
| 70 |
+
path = request.scope.get("path")
|
| 71 |
+
if path and "//" in path:
|
| 72 |
+
request.scope["path"] = path.replace("//", "/")
|
| 73 |
+
return await call_next(request)
|
| 74 |
+
|
| 75 |
app.add_middleware(
|
| 76 |
CORSMiddleware,
|
| 77 |
allow_origins=settings.cors_allowed_origins or ["*"],
|