diff --git a/.gitignore b/.gitignore index eade5d0..2ca4edd 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ __pycache__/ instance/ *.pyc -.env \ No newline at end of file +.env +*.db \ No newline at end of file diff --git a/app/__init__.py b/app/__init__.py index fd1324b..863e1f9 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,12 +1,24 @@ from flask import Flask from flask_socketio import SocketIO +from .db import close_db, init_db + sIO = SocketIO() def create_app(): app = Flask(__name__) - app.config['SECRET_KEY'] = 'dev' - + app.config['SECRET_KEY'] = 'dev' #! ofc not intended for prod use + + app.teardown_appcontext(close_db) + sIO.init_app(app) + from .routes.auth import auth_bp + from .routes.main import main_bp + + app.register_blueprint(auth_bp) + app.register_blueprint(main_bp) + + init_db(app) + return app \ No newline at end of file diff --git a/app/db.py b/app/db.py new file mode 100644 index 0000000..426182c --- /dev/null +++ b/app/db.py @@ -0,0 +1,35 @@ +import sqlite3 +from flask import g + +DATABASE = 'sqlite.db' + +def get_db(): + if 'db' not in g: + g.db = sqlite3.connect(DATABASE) + g.db.row_factory = sqlite3.Row + return g.db + +def close_db(e=None): + db = g.pop('db', None) + + if db is not None: + db.close() + +def init_db(app): + with app.app_context(): + db = get_db() + db.execute("PRAGMA journal_mode=WAL;") + db.execute("PRAGMA foreign_keys=ON;") + + # create inital tables + db.execute(""" + CREATE TABLE IF NOT EXISTS users ( + id INTEGER PRIMARY KEY AUTOINCREMENT,username TEXT UNIQUE NOT NULL, + password TEXT NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + ); + """ + ) + + + db.commit() \ No newline at end of file diff --git a/app/routes/auth.py b/app/routes/auth.py new file mode 100644 index 0000000..8a0944a --- /dev/null +++ b/app/routes/auth.py @@ -0,0 +1,57 @@ +from flask import Blueprint, render_template, request, redirect, url_for, session, flash +from app.db import get_db +from werkzeug.security import generate_password_hash, check_password_hash + +auth_bp = Blueprint("auth", __name__) + +@auth_bp.route("/login", methods=["GET", "POST"]) +def login(): + if request.method == "POST": + username = request.form["username"] + password = request.form["password"] + + db = get_db() + user = db.execute("SELECT * FROM users WHERE username = ?", (username,)).fetchone() + + if user and check_password_hash(user["password"], password): + session["user_id"] = user["id"] + return redirect(url_for("main.main")) + else: + flash("Invalid username or password") + + return render_template("login.html") + + +@auth_bp.route("/register", methods=["GET", "POST"]) +def register(): + if request.method == "POST": + username = request.form.get("username") + password = request.form.get("password") + + if not username or not password: + flash("Please fill out all fields") + return render_template("register.html") + + db = get_db() + + existing_user = db.execute( + "SELECT id FROM users WHERE username = ?", + (username,) + ).fetchone() + + if existing_user: + flash("Username already taken") + return render_template("register.html") + + hashed_password = generate_password_hash(password) + + db.execute( + "INSERT INTO users (username, password) VALUES (?, ?)", + (username, hashed_password) + ) + db.commit() + + flash("Account created! Please log in.") + return redirect(url_for("auth.login")) + + return render_template("register.html") \ No newline at end of file diff --git a/app/routes/main.py b/app/routes/main.py new file mode 100644 index 0000000..b1b57dc --- /dev/null +++ b/app/routes/main.py @@ -0,0 +1,7 @@ +from flask import Blueprint, render_template + +main_bp = Blueprint("main", __name__) + +@main_bp.route("/", methods=["GET", "POST"]) +def main(): + return render_template("main.html") \ No newline at end of file diff --git a/app/static/style.css b/app/static/style.css new file mode 100644 index 0000000..4fed022 --- /dev/null +++ b/app/static/style.css @@ -0,0 +1,9 @@ +.link-button { + border: 1px solid black; + padding: 10px; + text-decoration: none; + border-radius: 5px; + background-color: lightgray; + color: black; + margin: 5px; +} diff --git a/app/templates/login.html b/app/templates/login.html new file mode 100644 index 0000000..8548872 --- /dev/null +++ b/app/templates/login.html @@ -0,0 +1,9 @@ +

Login

+ +
+ + + +
+ +Register diff --git a/app/templates/main.html b/app/templates/main.html new file mode 100644 index 0000000..88f5d51 --- /dev/null +++ b/app/templates/main.html @@ -0,0 +1,20 @@ + + + + + + Chess + + + +

Chess Game

+

main page

+ + Login + + Register + + diff --git a/app/templates/register.html b/app/templates/register.html new file mode 100644 index 0000000..5f85a62 --- /dev/null +++ b/app/templates/register.html @@ -0,0 +1,9 @@ +

Register

+ +
+ + + +
+ +Back to Login