Update main.py
Browse files
main.py
CHANGED
|
@@ -252,6 +252,57 @@ def register(
|
|
| 252 |
token = create_access_token(sub=str(email), scopes=["verifyAccount"], minutes=7)
|
| 253 |
return {"access_token": token, "token_type": "bearer"}
|
| 254 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
@app.post("/api/auth/resend_otp")
|
| 256 |
def resend_otp(payload=Depends(require_scopes(["verifyAccount"]))):
|
| 257 |
email = payload["sub"]
|
|
@@ -269,40 +320,91 @@ def resend_otp(payload=Depends(require_scopes(["verifyAccount"]))):
|
|
| 269 |
|
| 270 |
@app.post("/api/auth/check_otp")
|
| 271 |
def check_otp(body: EmailOTP, payload=Depends(require_scopes(["verifyAccount"]))):
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
if rec.get("consumed_at"): raise HTTPException(400, "OTP already used")
|
| 282 |
-
if parse_iso_utc(rec["expires_at"]) < now_utc():
|
| 283 |
-
raise HTTPException(400, "OTP expired")
|
| 284 |
-
if not pwd.verify(body.otp, rec["code_hash"]):
|
| 285 |
-
supa_update("user_otps", eq("id", rec["id"]), {"attempts": (rec.get("attempts") or 0) + 1})
|
| 286 |
-
raise HTTPException(400, "Wrong OTP")
|
| 287 |
|
| 288 |
-
|
| 289 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
|
| 295 |
@app.post("/api/auth/login")
|
| 296 |
def login(body: LoginIn):
|
| 297 |
-
rows = supa_select(
|
| 298 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
user = rows[0]
|
| 300 |
-
if not pwd.verify(body.password, user["password"]): raise HTTPException(400, "Bad credentials")
|
| 301 |
-
if not user.get("email_verified_at"): raise HTTPException(400, "Please verify your email first")
|
| 302 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
access = create_access_token(sub=user["email"], scopes=["user"], minutes=60*24)
|
| 304 |
refresh, _ = rotate_refresh(user["id"], device="mobile", ua=None)
|
| 305 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
|
| 307 |
@app.post("/api/auth/never_expire")
|
| 308 |
def never_expire(payload=Depends(require_scopes(["user"])), note: Optional[str] = Form(None)):
|
|
|
|
| 252 |
token = create_access_token(sub=str(email), scopes=["verifyAccount"], minutes=7)
|
| 253 |
return {"access_token": token, "token_type": "bearer"}
|
| 254 |
|
| 255 |
+
@app.post("/api/auth/register_test")
|
| 256 |
+
def register_test(
|
| 257 |
+
email: EmailStr = Form(...),
|
| 258 |
+
password: str = Form(...),
|
| 259 |
+
password_confirmation: str = Form(...),
|
| 260 |
+
fname: Optional[str] = Form(None),
|
| 261 |
+
lname: Optional[str] = Form(None),
|
| 262 |
+
mobile: Optional[str] = Form(None),
|
| 263 |
+
):
|
| 264 |
+
"""
|
| 265 |
+
نسخة اختبار من التسجيل — ترجع كود الـ OTP في الـ response.
|
| 266 |
+
⚠️ لا تُستخدم في الإنتاج لأنها تكشف كود التحقق.
|
| 267 |
+
"""
|
| 268 |
+
if password != password_confirmation:
|
| 269 |
+
raise HTTPException(422, "Passwords do not match")
|
| 270 |
+
|
| 271 |
+
existed = supa_select("users", {**eq("email", email), "select": "id", "limit": "1"})
|
| 272 |
+
if existed:
|
| 273 |
+
raise HTTPException(422, "Email already exists")
|
| 274 |
+
|
| 275 |
+
user = supa_insert("users", {
|
| 276 |
+
"email": email,
|
| 277 |
+
"password": pwd.hash(password),
|
| 278 |
+
"fname": fname,
|
| 279 |
+
"lname": lname,
|
| 280 |
+
"mobile": mobile,
|
| 281 |
+
"role": "client",
|
| 282 |
+
"is_active": True,
|
| 283 |
+
"created_at": iso(now_utc())
|
| 284 |
+
})
|
| 285 |
+
|
| 286 |
+
otp = f"{random.randint(100000, 999999)}"
|
| 287 |
+
supa_insert("user_otps", {
|
| 288 |
+
"user_id": user["id"],
|
| 289 |
+
"purpose": "verify",
|
| 290 |
+
"code_hash": pwd.hash(otp),
|
| 291 |
+
"expires_at": iso(now_utc() + timedelta(minutes=3)),
|
| 292 |
+
"attempts": 0,
|
| 293 |
+
"created_at": iso(now_utc())
|
| 294 |
+
})
|
| 295 |
+
|
| 296 |
+
send_otp_email(email, otp, "verify", user.get("fname"))
|
| 297 |
+
|
| 298 |
+
token = create_access_token(sub=str(email), scopes=["verifyAccount"], minutes=7)
|
| 299 |
+
return {
|
| 300 |
+
"access_token": token,
|
| 301 |
+
"token_type": "bearer",
|
| 302 |
+
"message": "تم إرسال كود OTP على بريدك الإلكتروني",
|
| 303 |
+
"otp": otp # ← للـ testing فقط
|
| 304 |
+
}
|
| 305 |
+
|
| 306 |
@app.post("/api/auth/resend_otp")
|
| 307 |
def resend_otp(payload=Depends(require_scopes(["verifyAccount"]))):
|
| 308 |
email = payload["sub"]
|
|
|
|
| 320 |
|
| 321 |
@app.post("/api/auth/check_otp")
|
| 322 |
def check_otp(body: EmailOTP, payload=Depends(require_scopes(["verifyAccount"]))):
|
| 323 |
+
try:
|
| 324 |
+
# تحقق من أن البريد في التوكن يطابق البريد المُرسل
|
| 325 |
+
if body.email.lower() != payload["sub"].lower():
|
| 326 |
+
return {"status": 400, "message": "التوكن غير صالح"}
|
| 327 |
+
|
| 328 |
+
# جلب المستخدم
|
| 329 |
+
u = supa_select("users", {
|
| 330 |
+
**eq("email", body.email),
|
| 331 |
+
"select": "id,email,fname,lname,mobile,role,email_verified_at",
|
| 332 |
+
"limit": "1"
|
| 333 |
+
})
|
| 334 |
+
if not u:
|
| 335 |
+
return {"status": 400, "message": "المستخدم غير موجود"}
|
| 336 |
+
|
| 337 |
+
user = u[0]
|
| 338 |
+
rec = latest_valid_otp(user["id"], "verify")
|
| 339 |
+
|
| 340 |
+
# تحقق من الكود
|
| 341 |
+
if not rec:
|
| 342 |
+
return {"status": 400, "message": "كود التحقق غير موجود"}
|
| 343 |
+
if rec.get("consumed_at"):
|
| 344 |
+
return {"status": 400, "message": "تم استخدام الكود من قبل"}
|
| 345 |
+
if parse_iso_utc(rec["expires_at"]) < now_utc():
|
| 346 |
+
return {"status": 400, "message": "انتهت صلاحية الكود"}
|
| 347 |
+
if not pwd.verify(body.otp, rec["code_hash"]):
|
| 348 |
+
supa_update("user_otps", eq("id", rec["id"]), {
|
| 349 |
+
"attempts": (rec.get("attempts") or 0) + 1
|
| 350 |
+
})
|
| 351 |
+
return {"status": 400, "message": "كود التحقق غير صحيح"}
|
| 352 |
+
|
| 353 |
+
# ✅ لو الكود صحيح
|
| 354 |
+
supa_update("user_otps", eq("id", rec["id"]), {
|
| 355 |
+
"consumed_at": iso(now_utc())
|
| 356 |
+
})
|
| 357 |
+
supa_update("users", eq("id", user["id"]), {
|
| 358 |
+
"email_verified_at": iso(now_utc())
|
| 359 |
+
})
|
| 360 |
|
| 361 |
+
access = create_access_token(sub=user["email"], scopes=["user"], minutes=60*24)
|
| 362 |
+
refresh, _ = rotate_refresh(user["id"], device="mobile", ua=None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 363 |
|
| 364 |
+
return {
|
| 365 |
+
"status": 200,
|
| 366 |
+
"message": "تم تفعيل حسابك بنجاح!",
|
| 367 |
+
"access_token": access,
|
| 368 |
+
"refresh_token": refresh,
|
| 369 |
+
"user": user
|
| 370 |
+
}
|
| 371 |
|
| 372 |
+
except Exception as e:
|
| 373 |
+
print("[check_otp] ERROR:", e)
|
| 374 |
+
return {"status": 400, "message": "رمز التحقق غير صحيح، يُرجى المحاولة مرة أخرى."}
|
| 375 |
|
| 376 |
@app.post("/api/auth/login")
|
| 377 |
def login(body: LoginIn):
|
| 378 |
+
rows = supa_select(
|
| 379 |
+
"users",
|
| 380 |
+
{**eq("email", body.email),
|
| 381 |
+
"select": "id,email,password,email_verified_at,fname,lname,mobile,role",
|
| 382 |
+
"limit": "1"}
|
| 383 |
+
)
|
| 384 |
+
|
| 385 |
+
# أي فشل => رسالة عامة (محمية ضد enumeration)
|
| 386 |
+
if not rows:
|
| 387 |
+
return {"is_verified": False, "message": "بيانات الدخول غير صحيحة"}
|
| 388 |
+
|
| 389 |
user = rows[0]
|
|
|
|
|
|
|
| 390 |
|
| 391 |
+
# تحقق من الباسورد والإيميل المفعل
|
| 392 |
+
if not pwd.verify(body.password, user["password"]):
|
| 393 |
+
return {"is_verified": False, "message": "بيانات الدخول غير صحيحة"}
|
| 394 |
+
if not user.get("email_verified_at"):
|
| 395 |
+
return {"is_verified": False, "message": "بيانات الدخول غير صحيحة"}
|
| 396 |
+
|
| 397 |
+
# ناجح → أنشئ توكنات وأعد البيانات
|
| 398 |
access = create_access_token(sub=user["email"], scopes=["user"], minutes=60*24)
|
| 399 |
refresh, _ = rotate_refresh(user["id"], device="mobile", ua=None)
|
| 400 |
+
|
| 401 |
+
return {
|
| 402 |
+
"is_verified": True,
|
| 403 |
+
"message": "تم تسجيل الدخول بنجاح",
|
| 404 |
+
"access_token": access,
|
| 405 |
+
"refresh_token": refresh,
|
| 406 |
+
"user": user
|
| 407 |
+
}
|
| 408 |
|
| 409 |
@app.post("/api/auth/never_expire")
|
| 410 |
def never_expire(payload=Depends(require_scopes(["user"])), note: Optional[str] = Form(None)):
|