Upload folder using huggingface_hub
Browse files- api/auth.py +16 -9
api/auth.py
CHANGED
|
@@ -174,21 +174,28 @@ async def login(
|
|
| 174 |
if not final_email or not final_password:
|
| 175 |
raise HTTPException(status_code=422, detail="Email and password required")
|
| 176 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
try:
|
| 178 |
response = supabase.auth.sign_in_with_password({"email": final_email, "password": final_password})
|
| 179 |
-
if not response.session:
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
if
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
else:
|
| 186 |
-
db_user = db_models.User(email=final_email, supabase_id=response.user.id, is_active=True)
|
| 187 |
-
db.add(db_user)
|
| 188 |
db.commit()
|
| 189 |
|
| 190 |
return {"access_token": response.session.access_token, "token_type": "bearer"}
|
| 191 |
except Exception as e:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
raise HTTPException(status_code=401, detail=f"Login failed: {str(e)}")
|
| 193 |
|
| 194 |
@router.post("/forgot-password")
|
|
|
|
| 174 |
if not final_email or not final_password:
|
| 175 |
raise HTTPException(status_code=422, detail="Email and password required")
|
| 176 |
|
| 177 |
+
# 1. Check if user exists locally first for better error messaging
|
| 178 |
+
db_user = db.query(db_models.User).filter(db_models.User.email == final_email).first()
|
| 179 |
+
if not db_user:
|
| 180 |
+
raise HTTPException(status_code=404, detail="User does not exist")
|
| 181 |
+
|
| 182 |
try:
|
| 183 |
response = supabase.auth.sign_in_with_password({"email": final_email, "password": final_password})
|
| 184 |
+
if not response.session:
|
| 185 |
+
raise HTTPException(status_code=401, detail="Invalid login credentials")
|
| 186 |
+
|
| 187 |
+
# Sync supabase_id if it's missing or different (though it should matches)
|
| 188 |
+
if not db_user.supabase_id or db_user.supabase_id != response.user.id:
|
| 189 |
+
db_user.supabase_id = response.user.id
|
|
|
|
|
|
|
|
|
|
| 190 |
db.commit()
|
| 191 |
|
| 192 |
return {"access_token": response.session.access_token, "token_type": "bearer"}
|
| 193 |
except Exception as e:
|
| 194 |
+
error_str = str(e).lower()
|
| 195 |
+
if "invalid login credentials" in error_str or "invalid_credentials" in error_str:
|
| 196 |
+
raise HTTPException(status_code=401, detail="Invalid login credentials")
|
| 197 |
+
|
| 198 |
+
logger.error(f"Login failed unexpectedly: {str(e)}")
|
| 199 |
raise HTTPException(status_code=401, detail=f"Login failed: {str(e)}")
|
| 200 |
|
| 201 |
@router.post("/forgot-password")
|