Spaces:
Runtime error
Runtime error
BuildTools
commited on
Commit
·
b68ff27
1
Parent(s):
32cd1b5
tst
Browse files
main.py
CHANGED
|
@@ -1,39 +1,50 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException
|
| 2 |
-
from pydantic import BaseModel
|
| 3 |
from pymongo import MongoClient
|
| 4 |
-
from
|
|
|
|
| 5 |
import hashlib
|
|
|
|
| 6 |
|
| 7 |
-
# Configuration MongoDB
|
| 8 |
-
client = MongoClient("mongodb+srv://lebaykserver:mVef8e5F1GRtbA14@test.6bzxp.mongodb.net/")
|
| 9 |
-
db = client["IA_SIGNATURE"]
|
| 10 |
-
users_collection = db["USER"]
|
| 11 |
-
|
| 12 |
-
# Créer une instance de FastAPI
|
| 13 |
app = FastAPI()
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
etablissement: str
|
|
|
|
| 20 |
|
| 21 |
-
# Fonction pour hasher le mot de passe
|
| 22 |
def hash_password(password: str) -> str:
|
| 23 |
return hashlib.sha256(password.encode()).hexdigest()
|
| 24 |
|
| 25 |
-
@app.post("/login
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Depends
|
|
|
|
| 2 |
from pymongo import MongoClient
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from bson.objectid import ObjectId
|
| 5 |
import hashlib
|
| 6 |
+
import os
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
+
# Connexion à MongoDB
|
| 11 |
+
MONGO_URL = "mongodb://localhost:27017"
|
| 12 |
+
client = MongoClient(MONGO_URL)
|
| 13 |
+
db = client["mydatabase"] # Remplace "mydatabase" par le nom de ta base
|
| 14 |
+
users_collection = db["users"]
|
| 15 |
+
|
| 16 |
+
# Modèle de connexion
|
| 17 |
+
class LoginModel(BaseModel):
|
| 18 |
+
name: str
|
| 19 |
etablissement: str
|
| 20 |
+
hashed_password: str
|
| 21 |
|
|
|
|
| 22 |
def hash_password(password: str) -> str:
|
| 23 |
return hashlib.sha256(password.encode()).hexdigest()
|
| 24 |
|
| 25 |
+
@app.post("/login")
|
| 26 |
+
def login(user: LoginModel):
|
| 27 |
+
hashed_password = hash_password(user.hashed_password)
|
| 28 |
+
found_user = users_collection.find_one({
|
| 29 |
+
"name": user.name,
|
| 30 |
+
"etablissement": user.etablissement,
|
| 31 |
+
"hashed_password": hashed_password
|
| 32 |
+
})
|
| 33 |
|
| 34 |
+
if not found_user:
|
| 35 |
+
raise HTTPException(status_code=401, detail="Identifiants invalides")
|
| 36 |
|
| 37 |
+
return {"message": "Connexion réussie", "user_id": str(found_user["_id"]) }
|
| 38 |
+
|
| 39 |
+
@app.post("/register")
|
| 40 |
+
def register(user: LoginModel):
|
| 41 |
+
if users_collection.find_one({"name": user.name, "etablissement": user.etablissement}):
|
| 42 |
+
raise HTTPException(status_code=400, detail="Utilisateur déjà existant")
|
| 43 |
|
| 44 |
+
user_data = {
|
| 45 |
+
"name": user.name,
|
| 46 |
+
"etablissement": user.etablissement,
|
| 47 |
+
"hashed_password": hash_password(user.hashed_password)
|
| 48 |
+
}
|
| 49 |
+
inserted_id = users_collection.insert_one(user_data).inserted_id
|
| 50 |
+
return {"message": "Utilisateur enregistré", "user_id": str(inserted_id)}
|