Update main.py
Browse files
main.py
CHANGED
|
@@ -412,6 +412,68 @@ def login(body: LoginIn):
|
|
| 412 |
"user": user
|
| 413 |
}
|
| 414 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 415 |
@app.post("/api/auth/never_expire")
|
| 416 |
def never_expire(payload=Depends(require_scopes(["user"])), note: Optional[str] = Form(None)):
|
| 417 |
"""
|
|
|
|
| 412 |
"user": user
|
| 413 |
}
|
| 414 |
|
| 415 |
+
@app.post("/api/auth/login_never_expire")
|
| 416 |
+
def login_never_expire(body: LoginIn):
|
| 417 |
+
"""
|
| 418 |
+
يسجّل دخول ويرجع توكن دائم (بدون exp).
|
| 419 |
+
- status: 200 عند النجاح، 400 عند الفشل.
|
| 420 |
+
- يوضّح نوع الخطأ (إيميل أو كلمة سر).
|
| 421 |
+
"""
|
| 422 |
+
# 🔹 تحقق من وجود المستخدم
|
| 423 |
+
rows = supa_select(
|
| 424 |
+
"users",
|
| 425 |
+
{
|
| 426 |
+
**eq("email", body.email),
|
| 427 |
+
"select": "id,email,password,email_verified_at,fname,lname,mobile,role",
|
| 428 |
+
"limit": "1",
|
| 429 |
+
},
|
| 430 |
+
)
|
| 431 |
+
|
| 432 |
+
if not rows:
|
| 433 |
+
return {
|
| 434 |
+
"status": 400,
|
| 435 |
+
"is_verified": False,
|
| 436 |
+
"message": "البريد الإلكتروني غير صحيح"
|
| 437 |
+
}
|
| 438 |
+
|
| 439 |
+
user = rows[0]
|
| 440 |
+
|
| 441 |
+
# 🔹 تحقق من كلمة السر
|
| 442 |
+
if not pwd.verify(body.password, user["password"]):
|
| 443 |
+
return {
|
| 444 |
+
"status": 400,
|
| 445 |
+
"is_verified": False,
|
| 446 |
+
"message": "كلمة المرور غير صحيحة"
|
| 447 |
+
}
|
| 448 |
+
|
| 449 |
+
# 🔹 تحقق من تفعيل البريد
|
| 450 |
+
if not user.get("email_verified_at"):
|
| 451 |
+
return {
|
| 452 |
+
"status": 400,
|
| 453 |
+
"is_verified": False,
|
| 454 |
+
"message": "الحساب غير مفعل، يُرجى التحقق من البريد الإلكتروني"
|
| 455 |
+
}
|
| 456 |
+
|
| 457 |
+
# ✅ نجاح → إنشاء توكن دائم بدون exp
|
| 458 |
+
token, rec = create_permanent_token_for_email(
|
| 459 |
+
email=user["email"],
|
| 460 |
+
scopes=["user"],
|
| 461 |
+
note="login_never_expire_success"
|
| 462 |
+
)
|
| 463 |
+
|
| 464 |
+
return {
|
| 465 |
+
"status": 200,
|
| 466 |
+
"is_verified": True,
|
| 467 |
+
"message": "تم تسجيل الدخول بنجاح",
|
| 468 |
+
"access_token": token, # ← دائم (no exp)
|
| 469 |
+
"token_type": "bearer",
|
| 470 |
+
"expires": "never",
|
| 471 |
+
"permanent_token_id": rec["id"],
|
| 472 |
+
"issued_at": rec.get("created_at"),
|
| 473 |
+
"user": user
|
| 474 |
+
}
|
| 475 |
+
|
| 476 |
+
|
| 477 |
@app.post("/api/auth/never_expire")
|
| 478 |
def never_expire(payload=Depends(require_scopes(["user"])), note: Optional[str] = Form(None)):
|
| 479 |
"""
|