diff --git a/app/__init__.py b/app/__init__.py index 962ebc1..5fd7acc 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,4 +1,5 @@ from flask import Flask +import os from flask_sqlalchemy import SQLAlchemy from flask_login import LoginManager @@ -8,8 +9,11 @@ login_manager = LoginManager() def create_app(): app = Flask(__name__) - app.config["SQLALCHEMY_DATABASE_URI"] = "" + BASE_DIR = os.path.abspath(os.path.dirname(__file__)) + + app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" + os.path.join(BASE_DIR, "app.db") app.config["SECRET_KEY"] = "dev-secret-key" + app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False db.init_app(app) login_manager.init_app(app) @@ -17,6 +21,9 @@ def create_app(): from .models.user import User + with app.app_context(): + db.create_all() + @login_manager.user_loader def load_user(user_id): return db.session.get(User, int(user_id)) diff --git a/app/__pycache__/__init__.cpython-311.pyc b/app/__pycache__/__init__.cpython-311.pyc index 2e91d73..7124496 100644 Binary files a/app/__pycache__/__init__.cpython-311.pyc and b/app/__pycache__/__init__.cpython-311.pyc differ diff --git a/app/app.db b/app/app.db new file mode 100644 index 0000000..479ce3a Binary files /dev/null and b/app/app.db differ diff --git a/app/models/__pycache__/user.cpython-311.pyc b/app/models/__pycache__/user.cpython-311.pyc index 656c1fa..0748aa0 100644 Binary files a/app/models/__pycache__/user.cpython-311.pyc and b/app/models/__pycache__/user.cpython-311.pyc differ diff --git a/app/models/user.py b/app/models/user.py index 106078a..1a70419 100644 --- a/app/models/user.py +++ b/app/models/user.py @@ -7,8 +7,9 @@ class User(UserMixin, db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, nullable=False) password = db.Column(db.String(255), nullable=False) # <- größer! - email = db.Column(db.String(120), unique=True, nullable=False) + email = db.Column(db.String(120), unique=True, nullable=True) created_at = db.Column(db.DateTime, server_default=db.func.now()) + last_login = db.Column(db.DateTime) def __repr__(self): return f"" diff --git a/app/routes/__pycache__/login_routes.cpython-311.pyc b/app/routes/__pycache__/login_routes.cpython-311.pyc index 0e5e154..1b6cba1 100644 Binary files a/app/routes/__pycache__/login_routes.cpython-311.pyc and b/app/routes/__pycache__/login_routes.cpython-311.pyc differ diff --git a/app/routes/__pycache__/main_routes.cpython-311.pyc b/app/routes/__pycache__/main_routes.cpython-311.pyc index 3c156e5..506fedf 100644 Binary files a/app/routes/__pycache__/main_routes.cpython-311.pyc and b/app/routes/__pycache__/main_routes.cpython-311.pyc differ diff --git a/app/routes/login_routes.py b/app/routes/login_routes.py index a7143d7..a849065 100644 --- a/app/routes/login_routes.py +++ b/app/routes/login_routes.py @@ -1,6 +1,6 @@ -from flask import Blueprint, render_template, request, url_for, redirect +from flask import Blueprint, render_template, request, url_for, redirect, flash from werkzeug.security import generate_password_hash, check_password_hash -from flask_login import login_user +from flask_login import login_user, login_required, logout_user from ..models.user import User from .. import db @@ -11,7 +11,6 @@ def register(): if request.method == "POST": username = request.form.get("username") password = request.form.get("password") - email = request.form.get("email") if User.query.filter_by(username=username).first(): return render_template("sign_up.html", error="Username already taken!") @@ -20,8 +19,7 @@ def register(): new_user = User( username=username, - password=hashed_password, - email=email + password=hashed_password ) db.session.add(new_user) @@ -47,3 +45,10 @@ def login(): return render_template("login.html", error="Invalid username or password") return render_template("login.html") + +@login_bp.route('/logout') +@login_required +def logout(): + logout_user() + flash('You have been logged out', 'info') + return redirect(url_for('login.login')) diff --git a/app/routes/main_routes.py b/app/routes/main_routes.py index c67ef5d..5909e0a 100644 --- a/app/routes/main_routes.py +++ b/app/routes/main_routes.py @@ -1,7 +1,13 @@ from flask import Blueprint, render_template +from flask_login import login_required, current_user main_bp = Blueprint("main", __name__) @main_bp.route("/") def home(): return render_template("index.html") + +@main_bp.route('/dashboard') +@login_required +def dashboard(): + return render_template('dashboard.html', user=current_user) diff --git a/app/templates/dashboard.html b/app/templates/dashboard.html new file mode 100644 index 0000000..96397ba --- /dev/null +++ b/app/templates/dashboard.html @@ -0,0 +1,51 @@ + + + + + + + + Dashboard + + + + + + +
+ + ``` +

Dashboard

+ +

Welcome back {{ current_user.username }} 👋

+ + + +
+
+ +
+
+ ``` + +
+ + + + \ No newline at end of file diff --git a/app/templates/index.html b/app/templates/index.html index cab8a34..3d2f07b 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -27,7 +27,7 @@ Dashboard Logout {% else %} - Login + Login {% endif %} diff --git a/app/templates/login.html b/app/templates/login.html index 419978c..70581a9 100644 --- a/app/templates/login.html +++ b/app/templates/login.html @@ -19,8 +19,8 @@

Login to your account

@@ -29,7 +29,7 @@

{{ error }}

{% endif %} -
+ diff --git a/app/templates/sign_up.html b/app/templates/sign_up.html index 09e9a04..51fcb5a 100644 --- a/app/templates/sign_up.html +++ b/app/templates/sign_up.html @@ -19,8 +19,8 @@

Create an account

@@ -29,7 +29,7 @@

{{ error }}

{% endif %} - +