login complete (fr)

This commit is contained in:
2026-01-15 01:35:44 +00:00
parent 6746925a05
commit 1a7e3bb02c
6 changed files with 102 additions and 25 deletions

View File

@@ -8,6 +8,10 @@ import aiomysql
import bcrypt
#import asyncio
# In this FastAPI example, we skip BaseModel verification cause NuxtJS does that for us
# In other words, this backend only recieves requests from nuxt js backend
# So we can trust it
class db:
pool = None
@@ -49,7 +53,7 @@ class register_model(BaseModel):
async def create_user(req: Request):
body = await req.json()
body = register_model(**body)
#body = register_model(**body)
# Auto exceptions give more verbose to Nuxt.
#try:
@@ -57,7 +61,7 @@ async def create_user(req: Request):
#except ValidationError:
# raise HTTPException(status_code=400, detail="Missing / invalid arguments")
body.password = bcrypt.hashpw(body.password.encode(), bcrypt.gensalt()).decode()
body["password"] = bcrypt.hashpw(body["password"].encode(), bcrypt.gensalt()).decode()
async with db.pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute("INSERT INTO users (`first_name`,`last_name`,`email`,`password`) VALUES (%s,%s,%s,%s)", (body.first_name, body.last_name, body.email, body.password))
@@ -71,12 +75,13 @@ class login_data(BaseModel):
async def login_endpoint(req: Request):
body = await req.json()
#print(body)
try:
login = login_data(**body)
#login = login_data(**body)
# ensure it's only email and password
user_data = await fetch_user_info(body["email"])
if (user_data is None):
raise HTTPException(status_code=401)
raise HTTPException(status_code=401, detail="User doesn't exist")
else:
pwd = user_data.get("password")
if (bcrypt.checkpw(body["password"].encode(), user_data.get("password").encode())):