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())):

View File

@@ -5,7 +5,7 @@ export default defineEventHandler(async (event) => {
if ( !body.email || !body.password )
throw createError({ statusCode: 400, message: "Bad form. Use e-mail and password" })
console.log(body)
const req = await fetch(conf.fastapi_url + "/login",
{
method: "POST",
@@ -13,11 +13,27 @@ export default defineEventHandler(async (event) => {
"Content-Type": "application/json",
},
body: JSON.stringify({
email: body.email!,
pwd: body.password!,
email: body.email,
password: body.password,
})
}
)
if (!req.ok) throw createError({ statusCode: req.status })
else {
const body = await req.json();
await setUserSession(event,{
user: {
name: body.first_name,
surname: body.last_name,
email: body.email
},
private: {
user_id: body.id
}
})
return;
}
})

View File

@@ -1,3 +1,4 @@
export default defineEventHandler((async) => {
export default defineEventHandler(async (event) => {
// log them out
await clearUserSession(event);
})