Spaces:
Sleeping
Sleeping
Commit ·
bccf9f9
1
Parent(s): 60f20a4
Add detailed error logging to register/login endpoints
Browse files- app/main.py +30 -17
app/main.py
CHANGED
|
@@ -77,27 +77,40 @@ def _auth_response(token: str, user: User):
|
|
| 77 |
|
| 78 |
@app.post("/api/auth/register")
|
| 79 |
def register(data: UserCreate, db: Session = Depends(get_db)):
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
|
| 94 |
@app.post("/api/auth/login")
|
| 95 |
def login(data: UserLogin, db: Session = Depends(get_db)):
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
|
| 103 |
@app.post("/api/auth/logout")
|
|
|
|
| 77 |
|
| 78 |
@app.post("/api/auth/register")
|
| 79 |
def register(data: UserCreate, db: Session = Depends(get_db)):
|
| 80 |
+
try:
|
| 81 |
+
if get_user_by_email(db, data.email):
|
| 82 |
+
raise HTTPException(status_code=400, detail="Email already registered")
|
| 83 |
+
hashed = get_password_hash(data.password)
|
| 84 |
+
user = User(
|
| 85 |
+
email=data.email,
|
| 86 |
+
hashed_password=hashed,
|
| 87 |
+
full_name=data.full_name,
|
| 88 |
+
)
|
| 89 |
+
db.add(user)
|
| 90 |
+
db.commit()
|
| 91 |
+
db.refresh(user)
|
| 92 |
+
token = create_access_token(data={"sub": str(user.id)})
|
| 93 |
+
return _auth_response(token, user)
|
| 94 |
+
except HTTPException:
|
| 95 |
+
raise
|
| 96 |
+
except Exception as e:
|
| 97 |
+
print(f"[REGISTER] Error: {type(e).__name__}: {e}")
|
| 98 |
+
raise HTTPException(status_code=500, detail=f"Registration failed: {type(e).__name__}: {e}")
|
| 99 |
|
| 100 |
|
| 101 |
@app.post("/api/auth/login")
|
| 102 |
def login(data: UserLogin, db: Session = Depends(get_db)):
|
| 103 |
+
try:
|
| 104 |
+
user = get_user_by_email(db, data.email)
|
| 105 |
+
if not user or not verify_password(data.password, user.hashed_password):
|
| 106 |
+
raise HTTPException(status_code=401, detail="Invalid email or password")
|
| 107 |
+
token = create_access_token(data={"sub": str(user.id)})
|
| 108 |
+
return _auth_response(token, user)
|
| 109 |
+
except HTTPException:
|
| 110 |
+
raise
|
| 111 |
+
except Exception as e:
|
| 112 |
+
print(f"[LOGIN] Error: {type(e).__name__}: {e}")
|
| 113 |
+
raise HTTPException(status_code=500, detail=f"Login failed: {type(e).__name__}: {e}")
|
| 114 |
|
| 115 |
|
| 116 |
@app.post("/api/auth/logout")
|