add register, login and main page with basic functionality
This commit is contained in:
+2
-1
@@ -2,4 +2,5 @@
|
|||||||
__pycache__/
|
__pycache__/
|
||||||
instance/
|
instance/
|
||||||
*.pyc
|
*.pyc
|
||||||
.env
|
.env
|
||||||
|
*.db
|
||||||
+14
-2
@@ -1,12 +1,24 @@
|
|||||||
from flask import Flask
|
from flask import Flask
|
||||||
from flask_socketio import SocketIO
|
from flask_socketio import SocketIO
|
||||||
|
from .db import close_db, init_db
|
||||||
|
|
||||||
|
|
||||||
sIO = SocketIO()
|
sIO = SocketIO()
|
||||||
|
|
||||||
def create_app():
|
def create_app():
|
||||||
app = Flask(__name__)
|
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)
|
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
|
return app
|
||||||
@@ -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()
|
||||||
@@ -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")
|
||||||
@@ -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")
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
<h1>Login</h1>
|
||||||
|
|
||||||
|
<form method="POST">
|
||||||
|
<input name="username" placeholder="Username" required />
|
||||||
|
<input name="password" type="password" placeholder="Password" required />
|
||||||
|
<button type="submit">Login</button>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<a href="{{ url_for('auth.register') }}">Register</a>
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title>Chess</title>
|
||||||
|
<link
|
||||||
|
rel="stylesheet"
|
||||||
|
href="{{ url_for('static', filename='style.css') }}"
|
||||||
|
/>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Chess Game</h1>
|
||||||
|
<p>main page</p>
|
||||||
|
|
||||||
|
<a href="{{ url_for('auth.login') }}" class="link-button"> Login </a>
|
||||||
|
|
||||||
|
<a href="{{ url_for('auth.register') }}" class="link-button"> Register </a>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
<h1>Register</h1>
|
||||||
|
|
||||||
|
<form method="POST">
|
||||||
|
<input name="username" placeholder="Username" required />
|
||||||
|
<input name="password" type="password" required />
|
||||||
|
<button type="submit">Create Account</button>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<a href="{{ url_for('auth.login') }}">Back to Login</a>
|
||||||
Reference in New Issue
Block a user