diff --git a/app/components/main_header.vue b/app/components/main_header.vue index 96fa574..13c1da4 100644 --- a/app/components/main_header.vue +++ b/app/components/main_header.vue @@ -15,11 +15,26 @@ const props = defineProps({ type: Boolean, required: false, default: true + }, + logged_in: { + type: Boolean, + required: false, + default: false } + + }) - + function logout() { + return fetch("/logout").then( + (response) => { + if (response.ok) { + window.location.reload; + } + } + ) + } @@ -33,10 +48,10 @@ const props = defineProps({ - + - + - Login + Login diff --git a/app/pages/index.vue b/app/pages/index.vue index f9fdb1d..47f7976 100644 --- a/app/pages/index.vue +++ b/app/pages/index.vue @@ -1,5 +1,7 @@ + diff --git a/server/fastapi_server/fastapi_server.py b/server/fastapi_server/fastapi_server.py index df8c615..3840394 100644 --- a/server/fastapi_server/fastapi_server.py +++ b/server/fastapi_server/fastapi_server.py @@ -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())): diff --git a/server/routes/login.post.ts b/server/routes/login.post.ts index 07af227..0b1c3fc 100644 --- a/server/routes/login.post.ts +++ b/server/routes/login.post.ts @@ -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; + } }) \ No newline at end of file diff --git a/server/routes/logout.ts b/server/routes/logout.ts index bc6887b..f82d0e7 100644 --- a/server/routes/logout.ts +++ b/server/routes/logout.ts @@ -1,3 +1,4 @@ -export default defineEventHandler((async) => { - +export default defineEventHandler(async (event) => { + // log them out + await clearUserSession(event); }) \ No newline at end of file