sign in system pretty much

This commit is contained in:
ralphi3
2026-06-18 13:48:25 +12:00
parent eda37b8ad1
commit 723c60fa22
6 changed files with 85 additions and 18 deletions
+41 -8
View File
@@ -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 flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash from werkzeug.security import generate_password_hash, check_password_hash
@@ -19,6 +21,16 @@ with app.app_context():
db.create_all() 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']) @app.route('/submit-form', methods=['POST'])
def handle_submission(): def handle_submission():
form_username = request.form.get('username') form_username = request.form.get('username')
@@ -41,21 +53,21 @@ def handle_submission():
@app.route('/users') @app.route('/users')
@login_required
def view_users(): def view_users():
all_users = User.query.all() all_users = User.query.all()
html_output = "<h2>Registered Accounts</h2><table border='1' cellpadding='10'>" html_output = "<h2>Registered Accounts</h2><table border='1' cellpadding='10'>"
html_output += "<tr><th>ID</th><th>Username</th><th>Email</th></tr>" html_output += "<tr><th>ID</th><th>Username</th><th>Email</th></tr>"
for user in all_users: for user in all_users:
html_output += f"<tr><td>{user.id}</td><td>{user.username}</td><td>{user.email}</td><td>{user.password_hash}</td></tr>" html_output += f"<tr><td>{user.id}</td><td>{user.username}</td><td>{user.email}</td></tr>"
html_output += "</table>" html_output += "</table>"
if not all_users: if not all_users:
return "<h3>No accounts found in the database yet.</h3>" return "<h3>No accounts found in the database yet.</h3>"
return html_output return html_output
@@ -71,15 +83,36 @@ def bivariate():
def about(): def about():
return render_template('about.html') return render_template('about.html')
@app.route('/signin') @app.route('/signin', methods=['GET', 'POST'])
def signin(): 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') return render_template('signin.html')
@app.route('/logout')
def logout():
session.clear()
return redirect(url_for('home'))
@app.route('/signup') @app.route('/signup')
def signup(): def signup():
return render_template('signup.html') return render_template('signup.html')
@app.route('/all_users') @app.route('/all_users')
@login_required
def all_users(): def all_users():
return render_template('all_users.html') return render_template('all_users.html')
Binary file not shown.
+14
View File
@@ -62,4 +62,18 @@ image {
border-color: black; border-color: black;
border-width: 2px; border-width: 2px;
border-style: groove; 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;
} }
+6 -2
View File
@@ -9,11 +9,15 @@
<body> <body>
<ul class="navbar"> <ul class="navbar">
<li><a href="{{ url_for('home') }}">Home</a></li> <li><a href="{{ url_for('home') }}">Home</a></li>
<li><a href="{{ url_for('bivariate') }}">Bivariate Data</a></li>
<li><a href="{{ url_for('about') }}">About</a></li> <li><a href="{{ url_for('about') }}">About</a></li>
<li><a href="{{ url_for('all_users') }}">all_users</a></li> {% if session.user_id %}
<li><a href="{{ url_for('bivariate') }}">Bivariate Data</a></li>
<li style="margin-left: auto; color: white; margin-top: 14px; margin-left: 900px;">Hi, {{ session.username }}</li>
<li style="margin-left: 20px;"><a href="{{ url_for('logout') }}">Sign Out</a></li>
{% else %}
<li style="margin-left: auto;"><a href="{{ url_for('signup') }}">Sign Up</a></li> <li style="margin-left: auto;"><a href="{{ url_for('signup') }}">Sign Up</a></li>
<li style="margin-left:0px"><a href="{{ url_for('signin') }}">Sign In</a></li> <li style="margin-left:0px"><a href="{{ url_for('signin') }}">Sign In</a></li>
{% endif %}
</ul> </ul>
<main> <main>
{% block content %}{% endblock %} {% block content %}{% endblock %}
+2
View File
@@ -5,6 +5,8 @@
<h1>homepage for math website wow</h1> <h1>homepage for math website wow</h1>
<h2>If you want to access the math topics on this website you must be signed into a account</h2>
</div> </div>
+22 -8
View File
@@ -1,13 +1,27 @@
{% extends 'base.html' %} {% extends 'base.html' %}
{% block content %} {% block content %}
<head> <h1>Sign in</h1>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Sign in page with sql database</h1>
</body>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul class="flash-messages">
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
<form action="{{ url_for('signin') }}" method="POST">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br><br>
<button type="submit">Sign In</button>
</form>
<p>Don't have an account? <a href="{{ url_for('signup') }}">Sign up</a></p>
{% endblock %} {% endblock %}