Spaces:
Runtime error
Runtime error
Update server.py
Browse files
server.py
CHANGED
|
@@ -8,6 +8,7 @@ from auth_handler import AuthManager
|
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
auth = AuthManager()
|
|
|
|
| 11 |
auth._init_db()
|
| 12 |
|
| 13 |
app.add_middleware(
|
|
@@ -78,3 +79,39 @@ async def get_history(username: str):
|
|
| 78 |
"labels": [r[1] for r in rows] # Extract just the timestamps
|
| 79 |
}
|
| 80 |
return formatted_data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
auth = AuthManager()
|
| 11 |
+
otp_storage = {}
|
| 12 |
auth._init_db()
|
| 13 |
|
| 14 |
app.add_middleware(
|
|
|
|
| 79 |
"labels": [r[1] for r in rows] # Extract just the timestamps
|
| 80 |
}
|
| 81 |
return formatted_data
|
| 82 |
+
|
| 83 |
+
@app.post("/request-reset")
|
| 84 |
+
async def request_reset(data: EmailData):
|
| 85 |
+
email = data.email
|
| 86 |
+
# Check if the pilot actually exists in Supabase
|
| 87 |
+
if not auth.email_exists(email):
|
| 88 |
+
return {"success": False, "message": "Callsign not found in deep space logs."}
|
| 89 |
+
|
| 90 |
+
# Generate a code and store it in memory
|
| 91 |
+
otp = str(random.randint(100000, 999999))
|
| 92 |
+
otp_storage[email] = otp
|
| 93 |
+
|
| 94 |
+
# Dispatch via Brevo
|
| 95 |
+
success = auth.send_otp_via_brevo(email, otp)
|
| 96 |
+
|
| 97 |
+
if success:
|
| 98 |
+
return {"success": True, "message": "Verification code dispatched to your hangar!"}
|
| 99 |
+
else:
|
| 100 |
+
return {"success": False, "message": "Comms failure: Brevo link interrupted."}
|
| 101 |
+
|
| 102 |
+
@app.post("/confirm-reset")
|
| 103 |
+
async def confirm_reset(data: AuthData):
|
| 104 |
+
email = data.email
|
| 105 |
+
otp = data.otp
|
| 106 |
+
|
| 107 |
+
# 1. Verify the code matches what we stored
|
| 108 |
+
if otp_storage.get(email) != otp:
|
| 109 |
+
return {"success": False, "message": "Invalid code. Authorization denied."}
|
| 110 |
+
|
| 111 |
+
# 2. Update the password in Supabase
|
| 112 |
+
success, msg = auth.reset_password(email, data.username, data.password)
|
| 113 |
+
|
| 114 |
+
if success:
|
| 115 |
+
del otp_storage[email] # Clear code from memory
|
| 116 |
+
|
| 117 |
+
return {"success": success, "message": msg}
|