diff --git a/db/database.py b/db/database.py new file mode 100644 index 0000000..e4e4b92 --- /dev/null +++ b/db/database.py @@ -0,0 +1,36 @@ +import os +import psycopg2 + + +class PostgresConnector: + """ + Simple PostgreSQL connector (single connection). + """ + + def __init__(self): + self.connection = psycopg2.connect( + host=os.getenv("POSTGRES_HOST", "localhost"), + port=os.getenv("POSTGRES_PORT", 5432), + user=os.getenv("POSTGRES_USER", "postgres"), + password=os.getenv("POSTGRES_PASSWORD", "postgres"), + database=os.getenv("POSTGRES_DB", "postgres"), + ) + self.connection.autocommit = False + + def execute(self, query, params=None, fetch=False): + with self.connection.cursor() as cursor: + cursor.execute(query, params) + + if fetch: + return cursor.fetchall() + + self.connection.commit() + + def executemany(self, query, param_list): + with self.connection.cursor() as cursor: + cursor.executemany(query, param_list) + self.connection.commit() + + def close(self): + if self.connection: + self.connection.close() \ No newline at end of file diff --git a/server/auth.py b/server/auth.py new file mode 100644 index 0000000..dc396f7 --- /dev/null +++ b/server/auth.py @@ -0,0 +1,12 @@ +from db.database import PostgresConnector + +class AuthManager: + def __init__(self, db: PostgresConnector, bcrypt): + self.db = db + self.bcrypt = bcrypt + + def register_user(self, username, password): + # Hash the password + hashed_password = self.bcrypt.generate_password_hash(password).decode("utf-8") + # Save the user to the database + self.db.save_user(username, hashed_password) \ No newline at end of file