diff --git a/app.py b/app.py index 934b302..7f58eb2 100644 --- a/app.py +++ b/app.py @@ -1,4 +1,6 @@ -from flask import Flask, render_template, redirect, request, url_for +from functools import wraps + +from flask import Flask, render_template, redirect, request, url_for, session, flash from flask_sqlalchemy import SQLAlchemy from werkzeug.security import generate_password_hash, check_password_hash @@ -19,6 +21,16 @@ with app.app_context(): db.create_all() +def login_required(view_func): + @wraps(view_func) + def wrapped(*args, **kwargs): + if 'user_id' not in session: + flash('Please sign in to view that page.') + return redirect(url_for('signin')) + return view_func(*args, **kwargs) + return wrapped + + @app.route('/submit-form', methods=['POST']) def handle_submission(): form_username = request.form.get('username') @@ -41,21 +53,21 @@ def handle_submission(): @app.route('/users') +@login_required def view_users(): all_users = User.query.all() - html_output = "

Registered Accounts

" html_output += "" - + for user in all_users: - html_output += f"" - + html_output += f"" + html_output += "
IDUsernameEmail
{user.id}{user.username}{user.email}{user.password_hash}
{user.id}{user.username}{user.email}
" - + if not all_users: return "

No accounts found in the database yet.

" - + return html_output @@ -71,15 +83,36 @@ def bivariate(): def about(): return render_template('about.html') -@app.route('/signin') +@app.route('/signin', methods=['GET', 'POST']) def signin(): + if request.method == 'POST': + form_username = request.form.get('username') + form_password = request.form.get('password') + + user = User.query.filter_by(username=form_username).first() + + if user is None or not check_password_hash(user.password_hash, form_password): + flash('Incorrect username or password.') + return render_template('signin.html'), 401 + + session['user_id'] = user.id + session['username'] = user.username + return redirect(url_for('home')) + return render_template('signin.html') + +@app.route('/logout') +def logout(): + session.clear() + return redirect(url_for('home')) + @app.route('/signup') def signup(): return render_template('signup.html') @app.route('/all_users') +@login_required def all_users(): return render_template('all_users.html') diff --git a/instance/accounts.db b/instance/accounts.db index ed5a8aa..a120199 100644 Binary files a/instance/accounts.db and b/instance/accounts.db differ diff --git a/static/style.css b/static/style.css index b05b885..9c8f6d6 100644 --- a/static/style.css +++ b/static/style.css @@ -62,4 +62,18 @@ image { border-color: black; border-width: 2px; border-style: groove; +} + +.flash-messages { + list-style-type: none; + margin: 0 0 16px 0; + padding: 0; +} + +.flash-messages li { + background-color: #fee2e2; + color: #991b1b; + padding: 10px 14px; + border-radius: 4px; + margin-bottom: 8px; } \ No newline at end of file diff --git a/templates/base.html b/templates/base.html index e3f45c9..b970225 100644 --- a/templates/base.html +++ b/templates/base.html @@ -9,11 +9,15 @@
{% block content %}{% endblock %} diff --git a/templates/index.html b/templates/index.html index 5bc9b8c..9ec8cdd 100644 --- a/templates/index.html +++ b/templates/index.html @@ -5,6 +5,8 @@

homepage for math website wow

+

If you want to access the math topics on this website you must be signed into a account

+ diff --git a/templates/signin.html b/templates/signin.html index ec9094b..8b14a56 100644 --- a/templates/signin.html +++ b/templates/signin.html @@ -1,13 +1,27 @@ {% extends 'base.html' %} {% block content %} - - - - - - -

Sign in page with sql database

- +

Sign in

+{% with messages = get_flashed_messages() %} + {% if messages %} + + {% endif %} +{% endwith %} + +
+ +

+ + +

+ + +
+ +

Don't have an account? Sign up

{% endblock %} \ No newline at end of file