login works backend
This commit is contained in:
@@ -1,21 +1,24 @@
|
||||
# fastapi_server/user_mgmt.py
|
||||
from . import db_setup as db
|
||||
import db_setup as db
|
||||
import bcrypt
|
||||
import asyncio
|
||||
from aiomysql import DictCursor
|
||||
import aiomysql
|
||||
|
||||
import db_setup as db
|
||||
|
||||
async def fetch_user_info(arg: str, type: str):
|
||||
"""Fetches user info.
|
||||
This kind of query is supposed to either return ONE user or nothing.
|
||||
"""
|
||||
if db.mode == "mysql":
|
||||
async with db.pool.acquire() as conn:
|
||||
async with conn.cursor(DictCursor) as cur:
|
||||
await cur.execute("SELECT * FROM users WHERE %s = %s", (type, arg))
|
||||
async with db.mysql_pool.acquire() as conn:
|
||||
async with conn.cursor(aiomysql.DictCursor) as cur:
|
||||
await cur.execute(f"SELECT * FROM users WHERE {type} = %s", (arg,))
|
||||
#print(cur.description)
|
||||
return await cur.fetchone()
|
||||
elif db.mode == "sqlite":
|
||||
async with db.file.connect() as conn:
|
||||
async with conn.execute("SELECT * FROM users WHERE ? = ?", (type, arg)) as cursor:
|
||||
async with conn.execute(f"SELECT * FROM users WHERE {type} = ?", (arg,)) as cursor:
|
||||
return await cursor.fetchone()
|
||||
return None
|
||||
|
||||
@@ -23,14 +26,14 @@ async def fetch_user_info(arg: str, type: str):
|
||||
async def create_user(first_name: str, last_name: str, email: str, password: str):
|
||||
"""Creates a new user."""
|
||||
hashed_password = bcrypt.hashpw(password.encode(), bcrypt.gensalt())
|
||||
if db.db.mode == "mysql":
|
||||
async with db.db.pool.acquire() as conn:
|
||||
if db.mode == "mysql":
|
||||
async with db.mysql_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)", (first_name, last_name, email, hashed_password.decode()))
|
||||
await conn.commit()
|
||||
return True
|
||||
elif db.db.mode == "sqlite":
|
||||
async with db.db.file.connect() as conn:
|
||||
elif db.mode == "sqlite":
|
||||
async with db.file.connect() as conn:
|
||||
await conn.execute("INSERT INTO users (`first_name`,`last_name`,`email`,`password`) VALUES (?, ?, ?, ?)", (first_name, last_name, email, hashed_password.decode()))
|
||||
await conn.commit()
|
||||
return True
|
||||
@@ -40,6 +43,7 @@ async def create_user(first_name: str, last_name: str, email: str, password: str
|
||||
async def login_user(email: str, password: str):
|
||||
"""Logs in a user."""
|
||||
user_data = await fetch_user_info(email, "email")
|
||||
print(user_data)
|
||||
if user_data is None:
|
||||
return None # User doesn't exist
|
||||
else:
|
||||
@@ -56,7 +60,7 @@ async def edit_user(user_id: int, updates: dict):
|
||||
values = list(updates.values())
|
||||
query = f"UPDATE users SET {', '.join([f'{key} = %s' for key in updates.keys()])} WHERE id = %s"
|
||||
values.append(user_id)
|
||||
async with db.pool.acquire() as conn:
|
||||
async with db.mysql_pool.acquire() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute(query, values)
|
||||
await conn.commit()
|
||||
|
||||
Reference in New Issue
Block a user